2.Array implementation of stack
Sun Apr 26 2026 13:56:00 GMT+0000 (Coordinated Universal Time)
Saved by
@jenny
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
// Push operation
void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = value;
printf("Pushed %d\n", value);
}
// Pop operation
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
return;
}
printf("Popped %d\n", stack[top--]);
}
// Display stack
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;
do {
printf("\n--- STACK MENU ---\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
content_copyCOPY
Comments