题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <iostream>
#include <string>
using namespace std;
class Stackqkf {
private:
int qStack [100000];
int StackLocation = -1;
public:
void push (int x);
void pop(int x);
void top(int x);
};
void Stackqkf:: push (int x) {
StackLocation++;
qStack[StackLocation] = x;
}
void Stackqkf:: pop(int x) {
if (StackLocation >= 0) {
cout << qStack[StackLocation] << endl;
StackLocation--;
} else
cout << "error" << endl;
}
void Stackqkf:: top(int x) {
if (StackLocation >= 0)
cout << qStack[StackLocation] << endl;
else
cout << "error" << endl;
}
int main() {
int a, c;
string b;
Stackqkf qkf;
cin >> a;
while (a) {
a--;
cin >> b;
if (b == "push") {
cin >> c ;
qkf.push(c);
}
else if (b == "pop")
qkf.pop(c);
else
qkf.top(c);
}
}