Implement Stack
Easyjavapythonccppjavascript
Implement a stack that supports push, pop, peek and empty operations, processing a stream of operations.
Input Format
The first line contains an integer Q, the number of operations. Each of the next Q lines is one of: push X — push integer X pop — remove and return the top element peek — return the top element without removing it empty — return whether the stack is empty
Output Format
For each pop or peek, print the element (or -1 if the stack is empty). For each empty, print 1 if the stack is empty, otherwise 0. Print one value per line.
Example 1
Input
4 push 10 push 20 peek pop
Output
20 20
Explanation: 20 is on top, so both peek and pop return 20.
- 1 <= Q <= 100000
- -1000000000 <= X <= 1000000000