#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define MAX 10
void process_stack(int n) {
// Your logic here
int stack[MAX],index=0;
for(int i=0;i<n;i++){
char str[100];
fgets(str,sizeof(str),stdin);
if(strncmp(str, "push", 4) == 0){
if(index==MAX){
printf("Stack Overflow\n");
continue;
}
else{
int j=0;
int int_part=0;
while(str[j]!='\0'){
if(str[j]>='0' && str[j]<='9'){
int_part=int_part*10+(str[j]-'0');
}
j++;
}
stack[index++]=int_part;
}
}
else{
if(index<=0){
printf("Stack Underflow\n");
continue;
}
else{
printf("%d\n",stack[index-1]);
index--;
}
}
}
}
int main() {
int n;
scanf("%d", &n);
getchar(); // Consume newline after number
process_stack(n);
return 0;
}
simple conversion and stack concepts helped me to solve the problem
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow