PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY

/*PROGRAM TO PERFORM POP OPERATION ON STACK USING ARRAY*/
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define MAX 5
void main()
{
int maxstk,top=-1,n,a[MAX],i;
char ch;
clrscr();
//PUSH OPERATION
cout<<“Pushing elements into the stack: “<<endl;
do
{
if(top>=MAX-1)
{
cout<<“Stack is overflow”<<endl;
break;
}
else
{
top++;
cout<<“Enter element to be pushed: “;
cin>>n;
a[top]=n;
}
cout<<“Do you want to push more elements?? “;
cin>>ch;
}while(ch==’y’);
cout<<“After push operation elements of the stack are:”<<endl;
for(i=0;i<=top;i++)
{
cout<<a[i]<<“\t”;
}
cout<<endl<<“Do you want to perform pop operation?? “;
cin>>ch;
if(ch==’y’)
{
//POP OPERATION
do
{
if(top<0)
{
cout<<“Stack is underflow”;
break;
}
else
{
n=a[top];
cout<<“Element poped is: “<<n<<endl;
top–;
}
cout<<“Do you want to perform pop operation again?? “;
cin>>ch;
}while(ch==’y’);
if(top<0)
cout<<endl<<“Press any key to exit”<<endl;
else
{
cout<<“After pop operation elements of the stack are:”<<endl;
for(i=0;i<=top;i++)
{
cout<<a[i]<<“\t”;
}
}
}
getch();
}

Leave a Reply

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