// Implementing Static Stack using an Array in C #include <stdio.h> #include <stdlib.h> #include <stdbool.h> // N will be the capacity of the Static Stack #define N 1000 // Initializing the top of the stack to be -1 int top = -1; // Initializing the stack using an array int stack[N]; // Function prototypes void push(); // Push element to the top of the stack int pop(); // Remove and return the top most element of the stack int peek(); // Return the top most element of the stack bool isEmpty(); // Check if the stack is in Underflow state or not bool isFull(); // Check if the stack is in Overflow state or not int main(){ printf("STATIC ARRAY (Total Capacity: %d)\n", N); int choice; while(1){ printf("\nChoose any of the following options:\n"); printf(" 0: Exit 1: Push 2: Pop 3: Peek\n"); printf(" 4: Check if the stack is empty 5: Check if the stack is full\n\n"); scanf("%d", &choice); switch(choice){ case 0: exit(0); case 1: push(); break; case 2: pop(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; default: printf("Please choose a correct option!"); } } return 0; } void push(){ // Checking overflow state if(top == N-1) printf("Overflow State: can't add more elements into the stack\n"); else{ int x; printf("Enter element to be pushed into the stack: "); scanf("%d", &x); top+=1; stack[top] = x; } } int pop(){ // Checking underflow state if(top == -1) printf("Underflow State: Stack already empty, can't remove any element\n"); else{ int x = stack[top]; printf("Popping %d out of the stack\n", x); top-=1; return x; } return -1; } int peek(){ int x = stack[top]; printf("%d is the top most element of the stack\n", x); return x; } bool isEmpty(){ if(top == -1){ printf("Stack is empty: Underflow State\n"); return true; } printf("Stack is not empty\n"); return false; } bool isFull(){ if(top == N-1){ printf("Stack is full: Overflow State\n"); return true; } printf("Stack is not full\n"); return false; }