考察对于栈的各种操作,会用 STL 即可。
push进栈(如在 x 中添加 为x.push(1))pop出栈(如x.pop())top获取最上面的元素(如x.top())size大小(如x.size())
#include<bits/stdc++.h>
using namespace std;
stack<int> x;
signed main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
string s;
cin>>s;
if(s=="push"){
int m;
cin>>m;
x.push(m);
}else if(s=="pop"){
if(x.empty())cout<<"Empty"<<endl;
else x.pop();
}
else if(s=="query"){
if(x.empty())cout<<"Anguei!"<<endl;
else cout<<x.top()<<endl;
}else cout<<x.size()<<endl;
}
return 0;
}
暂无评论