#C3358. Implementing a Linked List Based Queue
Implementing a Linked List Based Queue
Implementing a Linked List Based Queue
Implement a queue using a linked list. The queue supports two operations: enqueue(x)
which adds an element \( x \) to the rear, and dequeue()
which removes and returns the element at the front. If the queue is empty, the dequeue()
operation should return \( -1 \).
Formally, if we denote the queue by \( \text{MyQueue} \), then the operations are defined as follows:
\( \text{enqueue}(x): \) Add element \( x \) to the rear of the queue.\( \text{dequeue}(): \) Remove and return the front element of the queue; if the queue is empty, return \( -1 \).</p>
inputFormat
The first line contains an integer \( n \) representing the number of operations.
Each of the next \( n \) lines contains an operation in one of the following forms:
enqueue x
— insert the integer \( x \) into the queue.dequeue
— remove the front element of the queue and print its value. If the queue is empty, print \( -1 \).
outputFormat
For each dequeue
operation, output the returned value on a separate line.
5
enqueue 1
enqueue 2
dequeue
enqueue 3
dequeue
1
2
</p>