-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationStack.cpp
More file actions
64 lines (59 loc) · 1.1 KB
/
SimulationStack.cpp
File metadata and controls
64 lines (59 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Problem: Simulation Stack
Description
Perform a sequence of operations over a stack, each element is an integer:
PUSH v: push a value v into the stack
POP: remove an element out of the stack and print this element to stdout (print NULL if the stack is empty)
Input
Each line contains a command (operration) of type
PUSH v
POP
Output
Write the results of POP operations (each result is written in a line)
Example
Input
PUSH 1
PUSH 2
PUSH 3
POP
POP
PUSH 4
PUSH 5
POP
#
Output
3
2
5
*/
//CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
string cmd;
int numb;
stack<int> st;
do {
cin >> cmd;
if (cmd == "#") {
continue;
}
if (cmd == "PUSH") {
cin >> numb;
st.push(numb);
}
if (cmd == "POP") {
if (st.empty()) {
cout << "NULL" << endl;
}
else {
int s = st.top();
st.pop();
cout << s << endl;
}
}
} while (cmd != "#");
return 0;
}