#include <cstdio>
#include <algorithm>
#include <stack>

using namespace std;

int main()
{
    stack <int> stk;
    int n;
    scanf("%d",&n);
    
    char cmd[10]={};
    for(int i=0; i<n; i++)
    {
        int x;
        scanf("%s",cmd);
        
        if(cmd[0]=='p')
        {
            if(cmd[1]=='u')
            {
                scanf("%d )",&x);
                stk.push(x);
            }
            else if(!stk.empty())
                stk.pop();
        }
            
        else if(cmd[0]=='t')
        {
            if(stk.empty())
                printf("-1\n");
            else
                printf("%d\n",stk.top());
        } 
            
        else if(cmd[0]=='s')
            printf("%d\n",stk.size());
            
        else
        {
            if(stk.empty())
                printf("true\n");
            else
                printf("false\n");
        }
    }
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: