题解 | 【模板】栈的操作
【模板】栈的操作
https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a
#include <iostream>
#include <array>
using namespace std;
class stack {
private:
array<int, 100000> arr;
int len = 0;
public:
void push(int x) {
len++;
arr[len-1] = x;
}
void pop() {
if (len > 0) {
len--;
} else {
cout << "Empty" << endl;
}
}
void query() {
if (len > 0) {
cout << arr[len-1] << endl;
} else {
cout << "Empty" << endl;
}
}
int size() {
return len;
}
};
int main() {
int op;
cin >> op;
stack stk;
while (op--) {
string s;
int num;
cin >> s;
if (s == "push") {
cin >> num;
stk.push(num);
} else if (s == "pop") {
stk.pop();
} else if (s == "query") {
stk.query();
} else if (s == "size") {
cout << stk.size() << endl;
}
}
}
