AcWing 828. 模拟栈
原题链接
简单
作者:
PeterYan
,
2024-01-10 14:42:55
,
所有人可见
,
阅读 31
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int st[N], tt = 0;
void push(int x)
{
st[tt++] = x;
}
int query()
{
if (tt <= 0) return -1;
return st[tt - 1];
}
void pop()
{
tt--;
}
bool empty()
{
if (tt <= 0) return true;
else return false;
}
int main()
{
int m, x;
cin >> m;
while(m--) {
string op;
cin >> op;
if(op == "push")
{
cin >> x;
push(x);
}
else if (op == "pop")
{
pop();
}
else if (op == "empty")
{
if (empty())
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}
else
{
cout << query() << endl;
}
}
return 0;
}