栈模拟
2023-08-16 10:31:05
发布于:河北
#include<bits/stdc++.h>
using namespace std;
int stk[505], tp;
void push(int x){
stk[++tp] = x;
}
void pop(){
tp--;
}
bool empty(){
return tp == 0;
}
int top(){
return stk[tp];
}
int size(){
return tp;
}
int main(){
int n; cin>>n;
while(n--){
string s; cin>>s;
if(s == "push"){
int x; cin>>x;
push(x);
}else if(s == "pop"){
if(empty()){
puts("pop fail");
}else{
cout << "pop " << top() << endl;
pop();
}
}else if(s == "top"){
if(empty()){
puts("top fail");
}else{
cout << "top = " << top() << endl;
}
}else if(s == "size"){
cout << "size = " << size() << endl;
}else if(s == "empty"){
if(empty()) puts("yes");
else puts("no");
}
}
return 0;
}
全部评论 4
老师好厉害,老师好棒
2023-08-16 来自 河北
0老师好厉害,老师好棒
2023-08-16 来自 河北
0老师好厉害,老师好棒
2023-08-16 来自 河北
0老师好厉害,老师好棒
2023-08-16 来自 河北
0
有帮助,赞一个