Menu driven program for Stack operations (Using Linked List)
Menu driven program for Stack operations (Using Linked List)
Write a menu driven program for Stack operations (Using Linked List)
a) Creation
of Stack
b)
Traversing of Stack
c) Push Operation
d)Pop
Operation
#include
<bits/stdc++.h>
using
namespace std;
struct Node
{
int data;
struct Node *next;
}*top=NULL;
void
Push(int x)
{
struct Node *t;
t=new Node;
if(t==NULL)
{
cout<<"Stack is full";
}
else
{
t->data=x;
t->next=top;
top=t;
}
}
int Pop()
{
struct Node *t;
int x=-1;
if(top==NULL)
cout<<"Stack is empty";
else
{
x=top->data;
t=top;
top=top->next;
delete t;
}
return x;
}
void
display()
{
struct Node *t;
t=top;
while(t)
{
cout<<t->data<<"
";
t=t->next;
}
}
int main()
{
int option,n,pos,x,index,t;
do
{
cout<<"1. Push in
Stack"<<endl<<"2. Pop from Stack
"<<endl<<"3. Display"<<endl<<"4.
Exit"<<endl;;
cout<<"Enter an
option"<<endl;
cin>>option;
switch(option)
{
case 1:
{
cout<<"Enter element :
";
cin>>x;
Push(x);
cout<<endl;
break;
}
case 2:
{
cout<<"Popped element
is: "<<Pop();
cout<<endl;
break;
}
case 3:
{
cout<<"Displaying
elements :";
display();
cout<<endl;
break;
}
default:
cout<<"Exiting
program......"<<endl;
}
}while(option<=3);
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 .