-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
58 lines (48 loc) · 1.34 KB
/
Stack.c
File metadata and controls
58 lines (48 loc) · 1.34 KB
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
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
int perform, x, top = -1;
printf("\n1. PUSH\n2. POP\n3. DISPLAY\n4. EXIT\n");
while(1) {
printf("Enter choice: ");
scanf("%d", &perform);
if (perform == 1) {
if (top == n - 1) {
printf("Stack is overflow\n");
} else {
printf("Enter the value: ");
scanf("%d", &x);
top++;
arr[top] = x;
}
}
else if (perform == 2) {
if (top == -1) {
printf("Stack is underflow\n");
} else {
printf("Deleted element is: %d\n", arr[top]);
top--;
}
}
else if (perform == 3) {
if (top == -1) {
printf("Stack is underflow\n");
} else {
printf("Stack elements are\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", arr[i]);
}
}
}
else if (perform == 4) {
break;
}
else {
printf("Invalid choice. Please enter 1, 2, 3, or 4.\n");
}
}
return 0;
}