Lab Program No. 3
Lab Program No. 3
Create a menu-driven program in C to perform various operations on a stack of integers using an array-based im
. Perform the following operations.
• Push an Element on to Stack.
• Pop an Element from Stack.
• Demonstrate Overflow and Underflow situations on Stack.
• Display the status of Stack.
• Exit
*/
#include <stdio.h>
#include <stdlib.h>
// Stack structure
typedef struct Stack {
int items[MAX];
int top;
} Stack;
// Main function
int main() {
Stack stack;
initStack(&stack);
int choice, value;
do {
printf("\nMenu:\n");
printf("1. Push an element onto stack\n");
printf("2. Pop an element from stack\n");
printf("3. Display stack status\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
display(&stack);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}