题解(数组版)
2024-08-05 17:59:45
发布于:广东
5阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int p[10005],TOP=0;
void push(int i){
p[++TOP]=i;
}
void pop(){
TOP--;
}
bool empty(){
return TOP==0;
}
int top(){
return p[TOP];
}
int size(){
return TOP;
}
int main(){
int n,a;
string b;
scanf("%d",&n);
while(n--){
cin>>b;
if(b=="push"){
cin>>a;
push(a);
}else if(b=="pop"){
if(empty()){
printf("pop fail\n");
continue;
}else {
printf("pop %d\n",top());
pop();
}
}else if(b=="top"){
if(empty()){
printf("top fail\n");
}else printf("top = %d\n",top());
}else if(b=="size"){
printf("size = %d\n",size());
}else if(b=="empty"){
if(empty()){
cout<<"yes"<<endl;
}else{
cout<<"no"<<endl;
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个