/
stack.cpp
98 lines (77 loc) · 1.44 KB
/
stack.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
#include <cstdio>
class Stack {
private:
struct Node {
int info;
Node *link;
};
typedef Node *Nodeptr;
Nodeptr front;
public:
Stack();
void push(int);
int pop();
int peep();
void display();
void menu();
};
Stack::Stack() {
front=NULL;
}
int Stack::peep() {
if(front==NULL) return '\0';
return front->info;
}
void Stack::push(int data) {
Nodeptr temp=new Node;
temp->info=data;
temp->link=front;
front=temp;
}
int Stack::pop() {
if(front==NULL) return 0;
Nodeptr temp=front;
front=front->link;
int val=temp->info;
delete temp;
return val;
}
void Stack::display() {
Nodeptr cursor=front;
while(cursor!=NULL) {
printf("%d->", cursor->info);
cursor=cursor->link;
}
printf("NULL\n");
}
void Stack::menu() {
int data;
for(;;) {
printf("Stack [ 1. Push | 2. Pop | 3. Peep | 4. Display ]: ");
scanf("%d", &data);
switch(data) {
case 1:
printf("Enter any no: "); scanf("%d", &data);
push(data);
display();
break;
case 2:
printf("%d deleted\n", pop());
display();
break;
case 3:
printf("%d peeped\n", peep());
break;
case 4:
display();
break;
default:
return;
}
}
}
int main (int argc, char const* argv[]) {
Stack s;
s.menu();
return 0;
}