-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.c
62 lines (57 loc) · 1.29 KB
/
Stack.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
60
61
62
#include<stdio.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top == MAX_SIZE - 1) {
printf("Overflow! & Exit\n");
} else {
stack[++top] = item;
printf("%d Item Inserted.\n", item);
}
}
void pop() {
if (top == -1) {
printf("Underflow & Exit\n");
} else {
int Item = stack[top--];
printf("%d Item Deleted. \n", Item);
}
}
void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
int choice, item;
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice!!\n");
}
}
return 0;
}