#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define MAX 10
typedef struct {
int buffer[MAX];
int index;
} my_stack;
my_stack stk = {.index = -1};
void push_stack(my_stack *stk, int x) {
if (stk -> index == MAX - 1) {
printf("Stack Overflow\n");
return;
} else {
stk -> index++;
stk -> buffer[stk -> index] = x;
}
};
void pop_stack(my_stack *stk) {
if (stk -> index == -1) {
printf("Stack Underflow\n");
return;
} else {
printf("%d\n", stk -> buffer[stk -> index]);
stk -> index--;
}
};
void process_stack(int n) {
// Your logic here
while (n--) {
char cmd[10];
scanf("%s", cmd); //fgets(cmd, sizeof(cmd), stdin);
if (strcmp(cmd, "push") == 0) {
int x; scanf("%d", &x);
push_stack(&stk, x);
} else if (strcmp(cmd, "pop") == 0) {
pop_stack(&stk);
} else return;
}
}
int main() {
int n;
scanf("%d", &n);
getchar(); // Consume newline after number
process_stack(n);
return 0;
}
Input
5 push 10 push 20 pop pop pop
Expected Output
20 10 Stack Underflow