题解 | #【模板】栈#

【模板】栈

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


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int st[N];
int n, x, top = -1;
string s;
int main()
{
    cin>>n;
    while (n--) {
        cin>>s;
        if (s == "push") {
            cin>>x;
            st[++top] = x;
        }else if (s == "top") {
            if (top == -1) {
                cout<<"error"<<endl;
            } else {
                cout<<st[top]<<endl;
            }
        }else if (s == "pop") {
            if (top == -1) {
                cout<<"error"<<endl;
            } else {
                cout<<st[top]<<endl;
                --top;
            }
        }
    }
    
    return 0;
}


全部评论

相关推荐

11 2 评论
分享
牛客网
牛客企业服务