Enter The Value To Be Pushed: 5 Enter The Value To Be Pushed: 6
Enter The Value To Be Pushed: 5 Enter The Value To Be Pushed: 6
#include<iostream.h>
#include<conio.h>
struct stu
{
int a;
stu *next;
};
stu *push(stu *start, int &val)
{
stu *x;
x=new stu;
x->a=val;
x->next=start;
start=x;
return(start);
}
stu *pop(stu *start, int val)
{
stu *ptr;
ptr=start;
if(ptr==NULL)
cout<<"no value can be popped";
else
{
start=start->next;
val=ptr->a;
delete ptr;
}
return start;
}
void traverse(stu *start)
{
stu *ptr;
ptr=start;
cout<<"the stack is...\n";
while(ptr!=NULL)
{
cout<<ptr->a<<" ";
ptr=ptr->next;
}
}
void main()
{clrscr();
stu *start;
intval,ch,i;
start=NULL;
for(i=0;i<2;i++)
{
cout<<"enter the value to be pushed : ";
cin>>val;
start = push(start,val);
}
do
{
cout<<"\n\nmenu\n1.push\n2.pop\n3.trav
erse\n4.quit\nenter your choice : ";
cin>>ch;
switch(ch)
{
case 1 : cout<<"enter the value to be
pushed : ";
cin>>val;
start=push(start, val);
break;
case 2 : start=pop(start, val);
cout<<"popped value is "<<val;
break;
case 3 : traverse(start);
break;
}
}
while(ch!=4);
getch();
}
menu
1.push
2.pop
3.traverse
4.quit
enter your choice : 3
the stack is...
65
menu
1.push
2.pop
3.traverse
4.quit
enter your choice : 1
enter the value to be pushed : 7
menu
1.push
2.pop
3.traverse
4.quit
enter your choice : 3
the stack is...
765
menu
1.push
2.pop
3.traverse
4.quit
enter your choice : 2
popped value is 7
menu
1.push
2.pop
3.traverse
4.quit
enter your choice : 3
the stack is...
65
menu
1.push
2.pop
3.traverse
4.quit
enter your choice :