#include <stdio.h> #include <stdint.h> #include <string.h> #define MAX 10 typedef struct { int buffer[MAX]; int head; } Stack; void initialize(Stack* s) { s->head = -1; } void push(Stack* s, int value){ if (s->head >= (MAX - 1)) { printf("Stack Overflow\n"); } else { s->head++; s->buffer[s->head] = value; } } void pop(Stack* s) { if (s->head < 0) { printf("Stack Underflow\n"); } else { printf("%d\n", s->buffer[s->head--]); } } void process_stack(int n) { // Your logic here Stack s = {0}; char cmd[10]; int value; initialize(&s); for (int i = 0; i < n; i++) { scanf("%s", cmd); if (strcmp(cmd, "push") == 0) { scanf("%d", &value); push(&s, value); } else { pop(&s); } } } int main() { int n; scanf("%d", &n); getchar(); // Consume newline after number process_stack(n); return 0; }
Stack is more simple than Circular Queue
my logic is similar to the solution
Test Cases
Test Results
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow