Write a C program to implement Static implementation of stack of integers with following operation: -Initialize (), push(), pop(), isempty(), isfull(), display()
#include <stdio.h>
#define MAX 100
int stack[MAX];
int top = -1;
// Function to initialize stack
void initialize() {
top = -1;
printf("Stack initialized.\n");
}
// Function to check if stack is empty
int isempty() {
return (top == -1);
}
// Function to check if stack is full
int isfull() {
return (top == MAX - 1);
}
// Function to push an element into stack
void push(int value) {
if(isfull()) {
printf("Stack Overflow! Cannot push %d\n", value);
return;
}
stack[++top] = value;
printf("%d pushed into stack.\n", value);
}
// Function to pop an element from stack
int pop() {
if(isempty()) {
printf("Stack Underflow! Cannot pop.\n");
return -1;
}
return stack[top--];
}
// Function to display stack elements
void display() {
if(isempty()) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements (top to bottom): ");
for(int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
// Main function to demonstrate stack operations
int main() {
int choice, value;
initialize();
do {
printf("\nStack Operations Menu:\n");
printf("1. Push\n2. Pop\n3. Display\n4. IsEmpty\n5. IsFull\n6. 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:
value = pop();
if(value != -1)
printf("Popped element: %d\n", value);
break;
case 3:
display();
break;
case 4:
if(isempty())
printf("Stack is empty.\n");
else
printf("Stack is not empty.\n");
break;
case 5:
if(isfull())
printf("Stack is full.\n");
else
printf("Stack is not full.\n");
break;
case 6:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while(choice != 6);
return 0;
}
