A program to implement Linked Stacks. The program has four options as given bellow and it continues till the user wants
a) Push
b) Pop
c) Display
d) Exit

 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

struct node
{
int data;
node *link;
};

node *start;
int top=0,size;

void push()
{
node *x;
if (top<size)
{
if(top==0)
{
start=new node;
cout<<"\nEnter data: ";
cin>>start->data;
start->link=NULL;
top++;
cout<<endl;
}
else
{
x=new node;
cout<<"\nEnter data: ";
cin>>x->data;
x->link=start;
start=x;
top++;
cout<<endl;
}
}
else
cout<<"\nStack Overflow.\n\n";
}

void pop()
{
node *x;
if(top>0)
{
x=start;
start=start->link;
x->link=NULL;
cout<<"\nDeleted value: "<<x->data<<"\n\n";
delete x;
top--;
}
else
cout<<"\nStack Underflow.\n\n";
}

void display()
{
node *x;
x=start;
cout<<endl;
cout<<"Data: ";
while(x!=NULL)
{
cout<<x->data<<" ";
x=x->link;
}
cout<<endl<<endl;
if(top==0)
cout<<"\r\rStack Empty.\n\n";
}

void main()
{
 clrscr();
 int ch;
 cout<<"Enter the size of the Link List: ";
 cin>>size;
 cout<<endl;

 do    
  {
   cout<<"1.Push"<<endl;
   cout<<"2.Pop"<<endl;
   cout<<"3.Display"<<endl;
   cout<<"4.Exit"<<endl;
   cout<<"Enter the choice: ";
   cin>>ch;

   switch(ch)
    {
     case 1:{
                push();
                break;
               }
     case 2:{
                pop();
                break;
               }
     case 3:{
                display();
                break;
               }
     case 4:{
                break;
               }
    default:{
                cout<<"\nWrong choice entered.\n\n";
                break;
               }
    }
  }
 while(ch!=4);
}

 

< Back