题解 | #【模板】栈#
【模板】栈
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(); } } }