-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_Arrays.c
80 lines (73 loc) · 1.8 KB
/
Stack_Arrays.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
int top = -1;
void Push(int stack[], int size, int data)
{
if (top == size - 1)
{
printf("Stack Overflow! Cannot push more elements.\n");
}
else
{
stack[++top] = data;
}
}
int Pop(int stack[])
{
if (top == -1)
{
printf("Stack Underflow! Cannot pop an element.\n");
}
else
{
return stack[top--];
}
return 0;
}
void Display(int stack[]){
if (top == -1) {
printf("Stack is empty. Nothing to display.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
printf("Enter the maximum size of the stack: ");
int size, data;
scanf("%d", &size);
int stack[size],choice;
do{
printf("\n1. Push\n2. Pop\n3. Display\n0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the data to push: ");
scanf("%d", &data);
Push(stack, size, data);
break;
case 2:
data = Pop(stack);
printf("Popped Element: %d", data);
break;
case 3:
Display(stack);
break;
case 0:
printf("\nExiting...\n\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice!= 0);
}
// void Peek(int stack[]){
// if (top == -1) {
// printf("Stack is empty. Nothing to peek.\n");
// } else {
// printf("Top element: %d\n", stack[top]);
// }
// }