#include <stdio.h> #include <stdint.h> #include <string.h> #define MAX 10 typedef struct{ int buffer[MAX]; int head; } Stack; void stack_init(Stack * s){ s->head = 0; } void stack_push(Stack * s, int x){ if(s->head == MAX){ printf("Stack Overflow\n"); return; } s->buffer[s->head] = x; s->head ++; } void stack_pop(Stack * s){ if(s->head <= 0){ printf("Stack Underflow\n"); return; } printf("%d\n", s->buffer[s->head-1]); s->head--; } int main() { int n; scanf("%d", &n); getchar(); // Consume newline after number Stack s; stack_init(&s); char cmd[10]; int val; while (scanf("%s", cmd) == 1) { if (strcmp(cmd, "push") == 0) { scanf("%d", &val); stack_push(&s, val); } else if (strcmp(cmd, "pop") == 0) { stack_pop(&s); } else { printf("Unknown command: %s\n", cmd); } } // process_stack(n); return 0; }
Test Cases
Test Results
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow