Program-15 Stack implementation using Array:Insertion and Deletion
#include<iostream.h>
#include<process.h>
#include<conio.h>
const int size=50;
int Push(int Stack[], int & top, int ele)
{
if(top == size-1) return -1;
else
{
top++;
Stack[top]=ele;
}
return 0;
}
int pop(int stack[], int &top)
{
int ret;
if(top == -1) return -1;
else
{
ret = stack[top];
top--;
}
return ret;
}
void display(int stack[], int top)
{
if(top==-1) return;
cout<<stack[top]<<"<-"<<endl;
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<endl;
}
int main()
{
int Stack[size],Item,top=-1,res;
char ch='y';
while(ch=='y'|| ch=='Y')
{
cout<<"\n Enter ITEm for insertion :";
cin>>Item;
res=Push(Stack,top,Item);
if(res==-1)
{
cout<<"Overflow !!! \n";
exit(1);
}
cout<<"The stack now is:\n";
display(Stack,top);
cout<<"\n want to insert more elements ?(y/n)";
cin>>ch;
}
cout<<"Now deletion of elements begins ...?\n";
cout<<"\n want to delete elements...?(y/n) \n";
cin>>ch;
while(ch=='y' || ch=='Y')
{
res=pop(Stack,top);
if(res==-1)
{
cout<<"Underflow \n";
exit(1);
}
else
{
cout<<"\n Element deleted is:"<<endl;
cout<<"\n The stack now is :\n";
display(Stack,top);
}
cout<<"\n want to delete more elements...?(y/n) \n";
cin>>ch;
}
return 0;
getch();
}
No comments:
Post a Comment