Stack Implementation
Implement a Stack class using an ArrayList as the internal storage. The stack should support push, pop, and peek operations.
Process commands from stdin and print results for pop and peek. If pop or peek is called on an empty stack, print Empty.
Input
- Line 1: number of commands N (1 <= N <= 2·10^4)
- For each command:
- Line 1: command type (
push,pop, orpeek) - Only for
push, line 2: the integer X (anyintvalue)
- Line 1: command type (
Behavior:
push X: push X onto the stackpop: remove and print the top elementpeek: print the top element without removing it
Output
- For each
pop: the removed value, orEmpty - For each
peek: the top value, orEmpty
Examples
Input:
6
push
10
push
20
peek
pop
pop
pop
Output:
20
20
10
Empty
Input:
3
push
5
push
15
peek
Output:
15
Input:
2
pop
peek
Output:
Empty
Empty
Both pop and peek must print Empty when the stack has nothing in it.