-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackOperation.c
53 lines (45 loc) · 1013 Bytes
/
StackOperation.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
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = value;
}
void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
printf("Popped: %d\n", stack[top--]);
}
void display() {
if (top == -1)
printf("Stack is empty\n");
else {
printf("Stack: ");
for (int i = 0; i <= top; i++)
printf("%d ", stack[i]);
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("1. Push 2. Pop 3. Display 4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter value : ");
scanf("%d", &value);
push(value);
} else if (choice == 2) {
pop();
} else if (choice == 3) {
display();
} else {
break;
}
}
return 0;
}