#C5650. Implement Queue Using Two Stacks
Implement Queue Using Two Stacks
Implement Queue Using Two Stacks
This problem requires you to implement a queue using two stacks. A queue is a First-In-First-Out (FIFO) data structure, and you are allowed only two stacks (Last-In-First-Out, LIFO) to simulate the operation of the queue. You need to support the following operations:
- enqueue x: Insert an element x into the queue.
- dequeue: Remove the element at the front of the queue and output it. It is guaranteed that this operation is never called on an empty queue.
- peek: Return the element at the front of the queue without removing it.
- is_empty: Check if the queue is empty and output
True
orFalse
.
You must implement the queue in such a way that all operations work efficiently. The underlying solution should use two stacks to mimic the behavior of the queue.
Note: The operations will be provided via standard input and the corresponding outputs should be printed to standard output.
inputFormat
The first line of input contains an integer n, denoting the number of operations. The next n lines each contain an operation which can be one of the following:
- "enqueue x" where x is an integer
- "dequeue"
- "peek"
- "is_empty" For operations that do not produce output (like enqueue), do not print anything.
outputFormat
For each operation that produces an output (dequeue, peek, is_empty), print the result on a new line. For boolean outputs, print either True or False.## sample
6
enqueue 1
enqueue 2
peek
dequeue
peek
is_empty
1
1
2
False
</p>