题解(最简单)
2024-03-21 21:05:05
发布于:广东
63阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int top;
int a[1010];
//入栈
void push(int x){
a[++top]=x;
}
void pop() {
--top;
}
int Top(){
return a[top];
}
int size(){
return top;
}
bool empty(){
return top==0;
}
int main(){
int n,x;
string s;
cin>>n;
while(n--){
cin>>s;
if(s=="empty"){
if(empty()) cout<<"yes";
else cout<<"no";
cout<<endl;
}
else if(s=="push"){
cin>>x;
push(x);
}
else if(s=="pop"){
if(!empty()){
cout<<"pop "<<Top();
pop();
}
else cout<<"pop fail";
cout<<endl;
}
else if(s=="top"){
if(!empty()) cout<<"top = "<<Top();
else cout<<"top fail";
cout<<endl;
}
else if(s=="size"){
cout<<"size = "<<size()<<endl;
}
}
return 0;
}
全部评论 1
其实有stl模版
2024-07-06 来自 广东
0
有帮助,赞一个