题解
2025-07-27 14:53:11
发布于:江苏
4阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
stack<int> st;//定义一个栈
int top = 0,t;
int main(){
    int n,data;cin >>n;
    while(n--){
        string s;cin >> s;
        if (s == "push"){
            cin >> t;
            st.push(t);
        }else if(s=="pop"){
            if (st.size()){//栈不为空,可以出栈
                cout <<"pop "<<st.top()<<endl;
                st.pop();
            }else{
                cout <<"pop fail"<<endl;
            }
        }else if(s=="top"){
            if (st.size()) cout << "top = "<<st.top()<<endl;
            else cout << "top fail"<<endl;
        }else if (s=="size"){
            cout << "size = "<<st.size()<<endl;
        }else if (s=="empty"){
            if (st.empty()){
                cout << "yes"<<endl;
            }else{
                cout << "no"<<endl;
            }
        }
    }
    return 0;
}
这里空空如也






有帮助,赞一个