题解 | 【模板】栈

【模板】栈

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

#include <iostream>
#include <string>
using namespace std;
class stack{
    private:
        int s[100000];
        int top_index = -1;
    public:
        void push(int x)
        {
            top_index += 1;
            s[top_index] = x;
        }

        void pop(){
           if(top_index >= 0){
             cout << s[top_index] << endl;
             top_index -=1;
           }
           else {
            cout << "error" <<  endl;
            }
        }
        void top(){
            if(top_index >=0){
                cout <<s[top_index] << endl;
            }
            else {
            cout << "error" <<  endl;
            }
        }

};
int main() {
    stack s;
    int n,x;
    cin >>n;
    for (int i = 0; i<n; i++) {
        string op;
        cin >> op;
        if (op =="push") {
            int x;
            cin >> x;
            s.push(x);
        }
        if (op == "pop") {
            s.pop();
        }
        if (op == "top") {
            s.top();
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

程序员花海_:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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