Menu driven program for Stack operations (Using Array)
Menu driven program for Stack operations (Using Array)
Menu driven program for Stack operations (Using Array)
a) Creation
of Stack
b)
Traversing of Stack
c)
Push Operation
d)Pop
Operation
#include
<bits/stdc++.h>
using
namespace std;
struct Stack
{
int size;
int top;
int *s;
};
void create(struct
Stack *st)
{
cout<<"Enter size :";
cin>>st->size;
st->top=-1;
st->s=new int [st->size];
}
void
Push(struct Stack *st,int x)
{
if(st->top==st->size-1)
cout<<"Stack
Overflow"<<endl;
else
{
st->top++;
st->s[st->top]=x;
}
}
int
Pop(struct Stack *st)
{
int x=-1;
if(st->top==-1)
cout<<"Stack
Underflow"<<endl;
else
{
x=st->s[st->top];
st->top--;
}
return x;
}
void
display(struct Stack st)
{
int i;
for(i=st.top;i>=0;i--)
{
cout<<st.s[i]<<"
";
}
cout<<endl;
}
int main()
{
struct Stack st;
int option,n,pos,x,index,t;
do
{
cout<<"1. Create
stack"<<endl<<"2. Insert/Push in
stack"<<endl<<"3. Delete/Pop
"<<endl<<"4. Display"<<endl<<"5.
Exit"<<endl;
cout<<"Enter an
option"<<endl;
cin>>option;
switch(option)
{
case 1 :
{
create(&st);
break;
}
case 2:
{
cout<<"Enter element :
"<<endl;
cin>>x;
Push(&st,x);
cout<<endl;
break;
}
case 3:
{
cout<<"Popped element is:
"<<Pop(&st);
cout<<endl;
break;
}
case 4:
{
cout<<"Displaying
elements :";
display(st);
cout<<endl;
break;
}
default:
cout<<"Exiting
program......"<<endl;
}
}while(option<=4);
return 0;
}
Comments
Post a Comment
Feel free to ask if you have any queries and if want any information . I feel happy to interact with you all and help you .