File tree Expand file tree Collapse file tree 1 file changed +84
-0
lines changed
Expand file tree Collapse file tree 1 file changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < process.h>
3+
4+ using namespace std ;
5+
6+ struct Node
7+ {
8+ int data;
9+ Node *next;
10+ }*top=NULL ,*p;
11+
12+ Node* newnode (int x)
13+ {
14+ p=new Node;
15+ p->data =x;
16+ p->next =NULL ;
17+ return (p);
18+ }
19+
20+ void push (Node *q)
21+ {
22+ if (top==NULL )
23+ top=q;
24+ else
25+ {
26+ q->next =top;
27+ top=q;
28+ }
29+ }
30+
31+ void pop (){
32+ if (top==NULL ){
33+ cout<<" Stack is empty!!" ;
34+ }
35+ else {
36+ cout<<" Deleted element is " <<top->data ;
37+ p=top;
38+ top=top->next ;
39+ delete (p);
40+ }
41+ }
42+
43+ void showstack ()
44+ {
45+ Node *q;
46+ q=top;
47+
48+ if (top==NULL ){
49+ cout<<" Stack is empty!!" ;
50+ }
51+ else {
52+ while (q!=NULL )
53+ {
54+ cout<<q->data <<" " ;
55+ q=q->next ;
56+ }
57+ }
58+ }
59+
60+ int main ()
61+ {
62+ int ch,x;
63+ Node *nptr;
64+ while (1 )
65+ {
66+ cout<<" \n\n 1.Push\n 2.Pop\n 3.Display\n 4.Exit" ;
67+ cout<<" \n Enter your choice(1-4):" ;
68+ cin>>ch;
69+ switch (ch){
70+ case 1 : cout<<" \n Enter data:" ;
71+ cin>>x;
72+ nptr=newnode (x);
73+ push (nptr);
74+ break ;
75+ case 2 : pop ();
76+ break ;
77+ case 3 : showstack ();
78+ break ;
79+ case 4 : exit (0 );
80+ default : cout<<" \n Wrong choice!!" ;
81+ }
82+ }
83+ return 0 ;
84+ }
You can’t perform that action at this time.
0 commit comments