-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
59 lines (52 loc) · 1.14 KB
/
Solution.c
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
#include <stdio.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1) {
printf("Stack overflow!\n");
} else {
stack[++top] = value;
printf("%d pushed into the stack.\n", value);
}
}
void pop() {
if (top == -1) {
printf("Stack underflow!\n");
} else {
printf("%d popped from the stack.\n", stack[top--]);
}
}
void peek() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Top element is: %d\n", stack[top]);
}
}
int main() {
int choice, value;
do {
printf("\n1. Push\n2. Pop\n3. Peek\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 4);
return 0;
}