数据结构:模拟栈
- 题目描述
- 参考代码
题目描述
输入样例
10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty
输出样例
5
5
YES
4
NO
参考代码
#include <iostream>
using namespace std;
const int N = 1000010;
int m, x;
int q[N];
string op;
int top;
void init()
{
top = 0;
}
void push(int x)
{
q[++top] = x;
}
void pop()
{
top--;
}
string empty()
{
if (top == 0) return "Yes";
return "No";
}
int query()
{
return q[top];
}
int main()
{
init();
cin >> m;
while (m--)
{
cin >> op;
if (op == "push")
{
cin >> x;
push(x);
}
else if (op == "pop")
{
pop();
}
else if (op == "query")
{
cout << query() << endl;
}
else
{
cout << empty() << endl;
}
}
return 0;
}