#K60067. Implement Queue Using Two Stacks
Implement Queue Using Two Stacks
Implement Queue Using Two Stacks
You are required to implement a queue using two stacks, and . The queue must support the following operations:
- : Insert the integer into the queue.
- : Remove the element from the front of the queue and return it. If the queue is empty, return .
When performing a dequeue operation, if is empty, transfer all elements from to and then pop from . Otherwise, simply pop from .
Process a series of commands provided via standard input as described below.
inputFormat
The first line contains an integer , representing the total number of operations. Each of the next lines contains a command. A command is either:
enqueue x
where x is an integer to be inserted into the queue.dequeue
which indicates that a dequeue operation should be performed.
For each dequeue
command, output the result on a new line.
outputFormat
For every dequeue
operation, output the returned integer on a separate line. If the queue is empty when a dequeue is requested, output -1.## sample
7
enqueue 2
enqueue 3
dequeue
enqueue 4
dequeue
dequeue
dequeue
2
3
4
-1
</p>