IMPLEMENT POP OPERATION ON STACK USING LINKED LIST

/*PROGRAM TO IMPLEMENT POP OPERATION ON STACK USING LINKED LIST*/
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *top=NULL,*ptr,*temp;
int num;
char ch;
clrscr();
//PUSHING ELEMENTS IN THE STACK
do
{
temp=new node;
cout<<“Enter the element to be pushed: “;
cin>>num;
temp->data=num;
temp->link=NULL;
if(top==NULL)
{
top=temp;
}
else
{
temp->link=top;
top=temp;
}
cout<<“Do you want to push more elements?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements in the stack are: “<<endl;
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
//POP OPERATION
cout<<endl<<“Do you want to perform pop operation on stack?? “;
cin>>ch;
if(ch==’y’||ch==’Y’)
{
do
{
if(top==NULL)
{
cout<<“Stack is empty”<<endl;
break;
}
else
{
num=top->data;
cout<<“Deleted element is: “<<num;
top=top->link;
}
cout<<endl<<“Do you want to pop more elements?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
}
else
{
exit(0);
}
if(top==NULL)
cout<<“Press any key to exit”;
else
{
cout<<“Remaining elements in the stack are: “<<endl;
for(ptr=top;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *