Home >>Cpp Programs >Implement stack with linked list in c++

Implement stack with linked list in c++

Implement Stack using Linked List in C++

In this example, we will see a C++ program through which we can implement a stack using a linked list.

To implement a stack using a linked list, basically, we will implement the push() and pop() operations of a stack using a linked list in this program.

Algorithm:

push() operation:

  • STEP 1: If the Linked list is empty then create a node and point it as head of that Linked List.
  • STEP 2: If the Linked List is not empty then create a node with the input number to be pushed and make it head of the Linked List.

pop() operation

  • STEP 1: If the Linked List is already empty then do nothing. Output that empty stack.
  • STEP 2: If the Linked List is not empty then delete the node from the head.
Example

#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
//Create a new node
struct node* create_node(int x){
struct node* temp= new node;
temp->data=x;
temp->next=NULL;
return temp;
}
//Enter the node into the linked list
void push(node** head,int x){
struct node* store=create_node(x);
if(*head==NULL){
*head =store;
return;
}
struct node* temp=*head;
//add the number in the front of the linked list
store->next=temp;
*head=store;
}
//pop from the stack
void pop(node** head){
if(*head==NULL)
return;
struct node* temp=(*head)->next;
*head=temp;				//delete from the front
}
void print(node* head){
struct node* temp=head;
while(temp){
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
struct node* l=NULL;
push(&l,1);
push(&l,2);
push(&l,3);
push(&l,4);
push(&l,5);
push(&l,6);
cout<<"Before the pop operation"<<endl;
print(l);
pop(&l);
pop(&l);
cout<<"\nAfter the pop operation"<<endl;
print(l);
return 0;
}

Output:
Before the pop operation
6 5 4 3 2 1
After the pop operation
4 3 2 1

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads