Professional Documents
Culture Documents
Solution A
Solution A
class Stack {
private:
struct Node {
int data;
Node* next;
Node* top;
public:
Stack()
: top(nullptr) {
}
~Stack() {
while (!isEmpty()) {
pop();
}
}
void pop() {
if (isEmpty()) {
return;
}
Node* temp = top;
top = top->next;
delete temp;
}
int peek() {
if (isEmpty()) {
return -1;
}
return top->data;
}
int main()
{
Stack stack;
int n;
cin >> n;
char sign;
int value;
Stack ans;
for (int i = 0; i < n; ++i){
cin >> sign;
if(sign == '+'){
cin >> value;
stack.push(value);
}
else{
ans.push(stack.peek());
stack.pop();
}
}
Stack tmp;
while (!ans.isEmpty()) {
tmp.push(ans.peek());
ans.pop();
}
while (!tmp.isEmpty()) {
cout << tmp.peek() << "\n";
tmp.pop();
}
return 0;
}