题解 | #【模板】栈#

【模板】栈

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

#include <iostream>
#include <string>
using namespace std;

class stack {
  private:
    int* data;
    int count;
  public:
    stack() {
        count = 0;
        data = new int[100000];
    }
    bool empty() {
        if (count == 0) {
            return true;
        }
        return false;
    }
    bool full() {
        if (count == 100000) {
            return true;
        }
        return false;
    }
    void push(int num) {
        if (full()) {
            cout << "error" << endl;
            return;
        }
        data[count] = num;
        count++;
    }
    void pop() {
        if (empty()) {
            cout << "error" << endl;
            return;
        }
        count--;
        cout << data[count] << endl;
    }
    void top() {
        if (empty()) {
            cout << "error" << endl;
            return;
        }
        cout << data[count - 1] <<endl;;
    }

};

int main() {
    stack s;
    string opt;
    int in;
    cin >> in;
    while (in--) {
        cin >> opt;
        if (opt == "push") {
            int num;
            cin >> num;
            s.push(num);
        } else if (opt == "pop") {
            s.pop();
        } else if (opt == "top") {
            s.top();
        }
    }
}

全部评论

相关推荐

程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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