#K84112. Queue via Two Stacks
Queue via Two Stacks
Queue via Two Stacks
This problem requires you to implement a queue using two stacks. The queue should support four operations: enqueue, dequeue, peek, and empty.
You must simulate the FIFO (First In First Out) behavior of a queue by using two stacks. Make sure to follow the operational semantics as described below.
Operations:
- enqueue(x): Add integer x to the end of the queue.
- dequeue(): Remove and return the element at the front of the queue.
- peek(): Return the front element of the queue without removing it.
- empty(): Return
True
if the queue is empty, otherwise returnFalse
.
inputFormat
The input is given via stdin and consists of multiple lines. The first line contains an integer (n) which denotes the number of operations. Each of the following (n) lines represents an operation which can be one of the following:
enqueue x
: where (x) is an integer.dequeue
peek
empty
Operations must be processed in the given order.
outputFormat
For each operation dequeue
, peek
, or empty
, output the result on a new line via stdout. The enqueue
operation does not produce any output. For empty
, output True
if the queue is empty, otherwise False
.## sample
5
enqueue 1
enqueue 2
peek
dequeue
empty
1
1
False
</p>