题解 | 【模板】栈

【模板】栈

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

#include <bits/stdc++.h>
using namespace std;

const int N = 100001;
int n,s[N],topIdx;

void push(int x){
    s[topIdx++] = x;
}

void top(){
    if(!topIdx) cout<<"error\n";
    else cout<<s[topIdx-1]<<"\n";
}


void pop(){
    if(!topIdx) cout<<"error\n";
    else cout<<s[--topIdx]<<"\n";
}


int main(){
    cin>>n;
    while(n--){
        string op;
        cin>>op;
        if("push"==op){
            int x;cin>>x;
            push(x);
        }
        else if("pop"==op){
            pop();
        }
        else{
            top();
        }
    }


    return 0;
}

#牛客春招刷题训练营#https://www.nowcoder.com/discuss/727521113110073344

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务