题解 | 【模板】栈的操作
【模板】栈的操作
https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a
#include <iostream>
#include<stack>
using namespace std;
void solve(){
stack<int> stk;
int t;
cin>>t;
string s1;
int num2;
while(t--){
cin>>s1;
if(s1 =="push"){
cin>>num2;
stk.push(num2);
}else if(s1 == "pop"){
if(stk.empty()){
cout<<"Empty"<<endl;
}else{
stk.pop();
}
}else if(s1 == "query"){
if(stk.empty()){
cout<<"Empty"<<endl;
}else{
cout<<stk.top()<<endl;
}
}else if(s1 == "size"){
cout<<stk.size()<<endl;
}
}
}
int main() {
solve();
return 0;
}
查看15道真题和解析