#include <stdio.
h>
#define MAX 100 // Maximum size of the stack
int stack[MAX], top = -1;
// Function to push an element onto the stack
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = value;
printf("%d pushed onto the stack.\n", value);
}
// Function to pop an element from the stack
void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return;
}
printf("%d popped from the stack.\n", stack[top--]);
}
// Function to display the stack elements
void display() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
int main() {
int choice, value;
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}