采用数组模拟栈,tt模拟栈顶指针
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N],tt = -1;
string str;
bool isempty(){
if (tt == -1)
return true;
else
return false;
}
void push(int x){
tt ++;
a[tt] = x;
}
void pop(){
tt --;
}
void query(){
cout << a[tt] <<endl;
}
int main(){
int n,x;
cin >> n;
while (n --){
cin >> str;
if (str == "push"){
cin >> x;
push(x);
}
else if (str == "pop"){
pop();
}
else if (str == "empty"){
if (isempty())
cout <<"YES"<<endl;
else
cout <<"NO" <<endl;
}
else if (str == "query"){
query();
}
}
}