-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathstack-linked-list.cpp
100 lines (92 loc) · 1.61 KB
/
stack-linked-list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Program to implement stacks as linked lists */
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
struct node {
int roll;
node* next;
} *top, *save, *ptr, *newptr, *np;
node *create(int a) {
ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}
void push(node *np) {
if (top==NULL) {
top=np;
}
else {
save=top;
top=np;
np->next=save;
}
}
void pop() {
if (top==NULL) {
cout<<"Underflow";
}
else {
ptr=top;
top=top->next;
delete ptr;
}
}
void display(node *np){
while (np!=NULL) {
cout<<np->roll<<"->";
np=np->next;
}
}
int main () {
top=NULL;
int m, n;
char k, ch;
do {
cout<<"\nChoose from the menu: ";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Quit";
cout<<endl<<"Enter your choice (1..4): ";
cin>>n;
switch(n) {
case 1: k='y';
while (k=='y' || k=='Y') {
cout<<"Enter element to be inserted: ";
cin>>m;
newptr=create(m);
if (newptr==NULL) {
cout<<"\n Cannot create!!";
}
else
push(newptr);
cout<<"\n The stack formed is: ";
display(top);
cout<<"\n Want to enter again ? ";
cin>>k;
}
break;
case 2: k='y';
while (k=='y' || k=='Y') {
pop();
cout<<"\n The stack formed is: ";
display(top);
cout<<"\n Want to delete again ? ";
cin>>k;
}
break;
case 3: cout<<"\n The stack formed is: ";
display(top);
break;
case 4: exit(0);
break;
default: cout<<"\n Please enter desired keyword: ";
}
cout<<"\n Do you want to continue ? ";
cin>>ch;
} while (ch=='y' || ch=='Y');
getch();
return 0;
}