-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
83 lines (76 loc) · 1.7 KB
/
Copy pathstack.c
File metadata and controls
83 lines (76 loc) · 1.7 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // maximum size of stack
int stack[MAX];
int top = -1;
// Push element
void push(int data) {
if (top == MAX - 1) {
printf("Stack Overflow! Cannot push %d\n", data);
return;
}
stack[++top] = data;
printf("Pushed %d onto stack.\n", data);
}
// Pop element
void pop() {
if (top == -1) {
printf("Stack Underflow! Nothing to pop.\n");
return;
}
printf("Popped %d from stack.\n", stack[top--]);
}
// Display stack
void display() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements (top -> bottom): ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
void peek() {
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Front element = %d\n", stack[top]);
}
}
// Menu-driven program
int main() {
int choice, data;
while (1) {
printf("\n--- Stack Menu ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}