#C1666. Queue using Two Stacks
Queue using Two Stacks
Queue using Two Stacks
You are required to implement a queue using two stacks. The queue should support four operations:
- enqueue x: Insert an integer x at the end of the queue.
- dequeue: Remove and return the element at the front of the queue. If the queue is empty, return -1.
- peek: Return the element at the front of the queue without removing it. If the queue is empty, return -1.
- isEmpty: Return whether the queue is empty. Output
True
if it is empty andFalse
otherwise.
Implement the above operations using two stacks so that the overall behavior is that of a FIFO (first-in-first-out) queue.
All operations that produce an output (i.e. dequeue
, peek
, and isEmpty
) should have their results printed in the order of execution, each on a new line.
inputFormat
The first line of input contains a single integer n (1 ≤ n ≤ 105), which is the number of operations to be performed on the queue.
Each of the following n lines contains one of the following commands:
enqueue x
where x is an integer.dequeue
peek
isEmpty
Note: The enqueue
command does not produce any output.
outputFormat
For each command that produces an output (dequeue
, peek
, isEmpty
), print the result on a new line in the order the commands are executed.
For dequeue
and peek
, if the queue is empty, output -1
.
For isEmpty
, output True
if the queue is empty, otherwise output False
.
7
enqueue 10
enqueue 20
peek
dequeue
npeek
dequeue
dequeue
10
10
20
-1
</p>