AcWing 828. 模拟栈
原题链接
简单
作者:
szywdwd
,
2021-05-13 18:52:17
,
所有人可见
,
阅读 191
#include <iostream>
#include <string>
using namespace std;
const int N = 100010;
int stk[N];
int top;// 顺序栈栈顶
// 初始化栈
void init() {
top = 0;
}
// 向栈顶插入一个数x
void push(int x) {
stk[top++] = x;
}
// 栈顶弹出元素
void pop() {
--top;
}
// 判断栈是否为空
bool empty() {
return !top;
}
// 查询栈顶元素
int query(){
return stk[top - 1];
}
int main()
{
int M;
cin >> M;
init();
while(M--) {
string op;
cin >> op;// cin读到空格自动截止
if(op == "push") {
int x;
cin >> x;
push(x);
}
else if(op == "pop") {
pop();
}
else if(op == "empty") {
if(empty()) cout << "YES" << endl;
else cout << "NO" << endl;
}
else if(op == "query") {
cout << query() << endl;
}
}
return 0;
}