INPUT
#include <stdio.h>
#define SIZE 5 // Maximum size of stack
int stack[SIZE], top = -1;
// Push opera on
void push(int value) {
if (top == SIZE - 1) {
prin ("Stack Overflow! Cannot push %d\n", value);
} else {
top++;
stack[top] = value;
prin ("%d pushed onto stack.\n", value);
}
}
// Pop opera on
void pop() {
if (top == -1) {
prin ("Stack Underflow! Nothing to pop.\n");
} else {
prin ("%d popped from stack.\n", stack[top]);
top--;
}
}
// Display opera on
void display() {
if (top == -1) {
prin ("Stack is empty.\n");
} else {
prin ("Stack elements (top to bo om): ");
for (int i = top; i >= 0; i--) {
prin ("%d ", stack[i]);
}
prin ("\n");
}
}
int main() {
int choice, value;
while (1) {
prin ("\n--- Stack Menu ---\n");
prin ("1. Push\n2. Pop\n3. Display\n4. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
prin ("Invalid choice! Try again.\n");
}
}
}
OUTPUT
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed onto stack.
Enter your choice: 1
Enter value to push: 20
20 pushed onto stack.
Enter your choice: 3
Stack elements (top to bo om): 20 10
Enter your choice: 2
20 popped from stack.
Enter your choice: 3
Stack elements (top to bo om): 10