#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define MAX 10
typedef struct s
{
int a[MAX];
int top;
int capacity;
}stack;
void process_stack(int n)
{
stack s; //s is a variable of type:stack which is defined above.
s.top=-1;
s.capacity=10;
char operation[8];
char *ptr;
while(n)
{
scanf(" %[^\n]",operation);
//logic for push operation.
ptr=strstr(operation,"push");
if(ptr!=NULL)
{
if(s.top==(s.capacity)-1) //Condition to check if stack is full.
{
printf("Stack Overflow\n");
}
else
{
int val;
ptr=strchr(operation,' ');
sscanf(ptr+1,"%d",&val);
//printf("DEBUG: val:%d\n",val);
(s.top)++;
s.a[s.top]=val;
//printf("DEBUG: Element inserted last at %d position with val:%d\n",s.top,val);
}
}
//logic for pop operation.
ptr=strstr(operation,"pop");
if(ptr!=NULL)
{
if(s.top==-1) //condition to check if stack is empty.
{
printf("Stack Underflow\n");
}
else
{
printf("%d\n",s.a[s.top]);
(s.top)--;
}
}
n--;
}
}
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