题目描述
数组模拟栈
C++代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int stk[N], t = -1;
bool empty(){
return t == -1;
}
void push(int x){
stk[++t] = x;
}
int pop(){
return stk[t--];
}
int query(){
return stk[t];
}
int main(){
int m, x;
string op;
cin >> m;
while(m--){
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;
}