0% found this document useful (0 votes)
36 views2 pages

Enter The Value To Be Pushed: 5 Enter The Value To Be Pushed: 6

This C++ program implements a linked stack data structure. It defines a struct node with an integer data field and pointer to the next node. Functions are defined to push and pop nodes onto/from the stack by updating the head pointer and allocating/deallocating memory. The main function initializes an empty stack, pushes sample values, and provides a menu to interactively push, pop and traverse the stack by calling the respective functions until the user chooses to quit.

Uploaded by

SurabhiSoumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Enter The Value To Be Pushed: 5 Enter The Value To Be Pushed: 6

This C++ program implements a linked stack data structure. It defines a struct node with an integer data field and pointer to the next node. Functions are defined to push and pop nodes onto/from the stack by updating the head pointer and allocating/deallocating memory. The main function initializes an empty stack, pushes sample values, and provides a menu to interactively push, pop and traverse the stack by calling the respective functions until the user chooses to quit.

Uploaded by

SurabhiSoumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* 21 linked stack*/

#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;
}
}

enter the value to be pushed : 5


enter the value to be pushed : 6

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 :

You might also like