#include #include #include // for bool, true, false #define MAX 5 // fixed size of the stack // Stack structure typedef struct { int data[MAX]; int top; } Stack; // Function prototypes void initialize(Stack *s); bool isFull(Stack *s); bool isEmpty(Stack *s); void push(Stack *s, int value); int pop(Stack *s); int size(Stack *s); // Initialize the stack void initialize(Stack *s) { s->top = -1; } // Check if stack is full bool isFull(Stack *s) { return s->top == MAX - 1; } // Check if stack is empty bool isEmpty(Stack *s) { return s->top == -1; } // Push element onto stack void push(Stack *s, int value) { if (isFull(s)) { printf("Stack overflow! Cannot push %d\n", value); return; } s->data[++(s->top)] = value; printf("Pushed %d\n", value); } // Pop element from stack int pop(Stack *s) { if (isEmpty(s)) { printf("Stack underflow! Cannot pop.\n"); return -1; // return sentinel value } int poppedValue = s->data[(s->top)--]; printf("Popped %d\n", poppedValue); return poppedValue; } // Return current size of stack int size(Stack *s) { return s->top + 1; } // Main function to demonstrate stack operations int main() { Stack s; initialize(&s); push(&s, 10); push(&s, 20); push(&s, 30); push(&s, 40); push(&s, 50); push(&s, 60); // overflow test printf("\nCurrent stack size: %d\n", size(&s)); pop(&s); pop(&s); printf("\nAfter popping, stack size: %d\n", size(&s)); if (isEmpty(&s)) printf("Stack is empty.\n"); else printf("Stack is not empty.\n"); if (isFull(&s)) printf("Stack is full.\n"); else printf("Stack is not full.\n"); return 0; }