Implement Stack using Queues

EasyDesign
Next
DesignStackQueue

Build a last-in-first-out stack using only queue operations. You are given a list of operations to run: push x adds x on top, pop removes and reports the top, top reports the top without removing it, and empty reports whether the stack is empty.

Input

Line 1: q, the number of operations. Next q lines: push x, pop, top or empty.

Output

For every pop and top print the value, and for every empty print true or false, one per line. Operations are always valid.

Constraints

1 <= q <= 10^4 1 <= x <= 99

Example 1
Input
5
push 1
push 2
top
pop
empty
Output
2
2
false

top shows 2, pop removes 2, and 1 is still in the stack so empty is false.

Hints

Implement Stack using Queues - Easy Design Problem | Skill Shiksha