#include <stdio.h> #include <stdint.h> #include <string.h> #define MAX 10 int arr[MAX]; int ptr = 0; bool isempty(){ return (ptr == 0); } bool isfull(){ return (ptr == MAX); } bool push(int x){ bool ret = true; if(isfull() == false){ arr[ptr] = x; ptr += 1; } else { printf("Stack Overflow\n"); //ret = false; } return ret; } bool pop(){ bool ret = true; if(isempty() == false){ ptr -= 1; printf("%d\n",arr[ptr]); }else{ printf("Stack Underflow\n"); ret = false; } return ret; } void process_stack(int n) { // Your logic here char str[10]; int val,res; bool ret = true; for(int i=0;(i<n);i++){ fgets(str,sizeof(str),stdin); res = sscanf(str,"push %d",&val); if(res>=1){ ret = push(val); }else if(strstr(str,"pop")!= NULL){ ret = pop(); } } } 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