#include <stdio.h> #include <stdint.h> #include <string.h> #define MAX 10 static int arr[MAX]; static int top =0; void push(int n); void pop(void); void process_stack(int n) { char cmd[20]; int value; for (int i = 0; i < n; i++) { fgets(cmd, sizeof(cmd), stdin); /* Check for push command */ if (strncmp(cmd, "push", 4) == 0) { /* Extract the number after "push" */ sscanf(cmd, "push %d", &value); push(value); } /* Check for pop command */ else if (strncmp(cmd, "pop", 3) == 0) { pop(); } } } int main() { int n; scanf("%d", &n); getchar(); // Consume newline after number process_stack(n); return 0; } void push(int n){ if(top==MAX){ printf("Stack Overflow\n"); }else{ arr[top++]=n; } } void pop(void){ if(top==0){ printf("Stack Underflow\n"); } else{ printf("%d\n",arr[--top]); } return ; }
Test Cases
Test Results
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow