#C13860. Implementing a Queue with Basic Operations
Implementing a Queue with Basic Operations
Implementing a Queue with Basic Operations
You are required to implement a queue data structure that supports the following operations:
- enqueue x: Add an element x to the end of the queue.
- dequeue: Remove and return the front element of the queue. If the queue is empty, return
None
. - front: Return the front element of the queue without removing it. If the queue is empty, return
None
. - size: Return the number of elements in the queue.
- empty: Return
True
if the queue is empty, otherwise returnFalse
.
The operations are given as commands. The enqueue
command is followed by an integer x. For operations that return a value (dequeue
, front
, size
, and empty
), output the corresponding result on a separate line.
Note: In terms of algorithm, the queue follows the FIFO (first-in-first-out) principle. Mathematically, if we denote the state of the queue as a sequence \(Q = [q_1, q_2, \ldots, q_n]\), then the operations can be described as follows:
- \(\text{enqueue}(x): Q \gets [q_1, q_2, \ldots, q_n, x]\)
- \(\text{dequeue}(): \text{if } n > 0, \text{ return } q_1 \text{ and update } Q \gets [q_2, \ldots, q_n], \text{ otherwise return } \texttt{None}\)
- \(\text{front}(): \text{if } n > 0, \text{ return } q_1, \text{ otherwise return } \texttt{None}\)
- \(\text{size}(): \text{ return } n\)
- \(\text{empty}(): \text{ return } \texttt{True} \text{ if } n=0 \text{, otherwise } \texttt{False}\)
inputFormat
The first line of input contains a single integer n (1 ≤ n ≤ 104), representing the number of commands. The following n lines each contain one command. The commands can be one of the following formats:
enqueue x
- where x is an integer.dequeue
front
size
empty
outputFormat
For each command that returns a value (dequeue
, front
, size
, and empty
), print the result on a new line. For the enqueue
command, no output is required.
If a command dequeue
or front
is executed on an empty queue, output None
(without quotes).
9
enqueue 1
enqueue 2
size
front
dequeue
dequeue
dequeue
size
empty
2
1
1
2
None
0
True
</p>