149. Stack Operations
Foundation1000 ms256 MBSolved by 0%
Simulate a stack. Read q operations, one per line:
1 x, pushxonto the stack2, pop the top value and print it3, print the top value without removing it
If a pop or a peek happens when the stack is empty, print Empty instead and carry on.
Print one line per 2 or 3 operation.
Constraints - `1 ≤ q ≤ 200000` - `-1000000 ≤ x ≤ 1000000`
Input
The first line contains an integer q, the number of operations.
Each of the next q lines is one operation:
- 1 x, push x
- 2, pop the top value and print it
- 3, print the top value without removing it
Output
Print one line for each 2 and 3 operation, the value, or Empty. A 1 prints nothing.
Input5
1 10
1 20
3
2
3
Output20
20
10
Notepushes twice then peeks, pops and peeks again
Input2
2
3
OutputEmpty
Empty
Notepops and peeks on an empty stack, which must not crash
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.