题解 | #【模板】栈#

【模板】栈

https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    int j = -1;
    cin >> n;
    int w[n];
    int i=n;
    string s;
    for(i=n;i>0;i--) {
        cin >> s;
        if(s=="push"){
            j++;
            cin >> w[j]; 
        }
        
        if(s=="pop"){
            if(j>=0) {
                cout << w[j] << endl;
                j--;
            }
            else cout << "error" << endl;
        }

        if(s=="top"){
            if(j>=0) {
                cout << w[j] << endl;
            }
            else cout << "error" << endl;
        }
    
    }
}

全部评论

相关推荐

头像
06-05 19:01
运营
点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务