Sunday, 5 October 2014

MENU DRIVEN STACK

/*push and pop in stack*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int top=-1,size,value,stack[100],i,option;  /*variable initiation*/
char choice;
cout<<"\n Enter stack size ";
cin>>size;
do
{
cout<<"\n press 1 for push ";
cout<<"\n       2 for pop ";
cin>>option;
if(option==1)
{   if(top==size-1)
{ cout<<"\noverflow";
exit(0);
}
else
{        cout<<"\n Enter value ";
cin>>value;
top=top+1;
stack[top]=value;
cout<<"stack..................\n";
for(i=top;i>=0;i--)
cout<<stack[i]<<endl;
}
}
if(option==2)
{  if(top==-1)                                                                                           /*no element for push*/
{ cout<<"\nunderflow";
exit(0);
}
if(top==0)
{
cout<<"\n element deleted";
top=top-1;
cout<<"stack..................";
for(i=top;i>=0;i--)
cout<<stack[top]<<endl;
}

{
}

}
cout<<"\n\n\nDo u want to continue press 1 ";
cout<<"\nEnter your choice ";
cin>>choice;
}while(choice=='1');
}

No comments:

Post a Comment