-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackinc
73 lines (64 loc) · 1.3 KB
/
stackinc
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
#include <stdio.h>
int top = -1;
int stack[10];
void push();
void pop();
void peep();
void disp();
int main() {
int n = 0;
while (n != 5) {
printf("\nEnter your choice: 1:push 2:pop 3:peep 4:display all 5:exit \n");
scanf("%d", &n);
switch (n) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
disp();
break;
case 5:
printf("\nExit");
break;
default:
printf("\nInvalid choice");
break;
}
}
return 0;
}
void push() {
if (top == 9) {
printf("\nOverflow");
} else {
top++;
printf("\nEnter an element\n");
scanf("%d", &stack[top]);
}
}
void pop() {
if (top == -1) {
printf("\nUnderflow");
} else {
printf("\nstack[%d]=%d element removed", top, stack[top]);
top--;
}
}
void peep() {
if (top >= 0) {
printf("\nstack[%d]=%d", top, stack[top]);
} else {
printf("\nEmpty");
}
}
void disp() {
for (int i = 0; i <= top; i++) {
printf("\nstack[%d]=%d", i, stack[i]);
}
}