#include <stdio.h> #include <stdint.h> #include <string.h> #define MAX 10 typedef struct { int buffer[MAX]; int head; int capacity; } Stack; void process_stack(int n) { char cmd[20] = {0}; int data; Stack stk = {0}; stk.capacity = n; while (n--) { scanf("%s %d\n", cmd, &data); if (!strncmp(cmd, "push", 4)) { if (stk.head > (MAX - 1)) { printf("Stack Overflow\n"); } else { stk.buffer[stk.head++] = data; } } else if (!strncmp(cmd, "pop", 3)) { if ((stk.head - 1) < 0) { printf("Stack Underflow\n"); } else { printf("%d\n", stk.buffer[--stk.head]); } } } } int main() { int n; scanf("%d", &n); getchar(); // Consume newline after number process_stack(n); return 0; }
Test Cases
Test Results
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow