The queue data structure (JS)(PY)
Last updated
Last updated
If you enjoy traveling (like I do), most likely you passed the check-in process at the airport. If there are a lot of travelers willing to check-in, naturally a queue of people is formed at the check-in desk.
A traveler who's just entered the airport and wants to check-in is going to enqueue into the queue. Another traveler that has just passed the check-in process at the desk is dequeued from the queue.
This is the real-world example of a queue — and the queue data structure works the same way.
The queue is a type of First Input-First Output (FIFO) data structure. The first enqueued item (input) is the first to dequeue (output).
A queue has 2 pointers: head and tail. The earliest enqueued item in the queue is at the head, while the latest enqueued item is at the tail of the queue.
Recalling the airport example, the traveler at the check-in desk is the head of the queue. The traveler who has just entered the queue is at the tail.
From a higher-point of view, the queue is the data structure that lets you process items, one at a time, in the same order they come in.
The queue supports 2 main operations: enqueue and dequeue. Additionally, you might find it useful to have the peek and length operations.
The enqueue operation inserts an item at the tail of the queue. The enqueued item becomes the tail of the queue.
The enqueue operation in the picture above inserts the item 8
at the tail. 8
becomes the tail of the queue.
The dequeue operation extracts the item at the head of the queue. The next item in the queue becomes the head.
In the picture above the dequeue operation returns and removes the item 7
from the queue. After dequeue, item 2
becomes the new head.
The peek operation reads the head of the queue, without altering the queue.
Item 7
is the head of the queue in the picture above. The peek operation simply returns the head — the item 7
— without modifying the queue.
Length operation counts how many items the queue contains.
The queue in the picture has 4 items: 4
, 6
, 2
, and 7
. As result, the queue length is 4
.
What's important regarding all of the queue operations — enqueue, dequeue, peek and length — all these operations must be performed in constant time O(1)
.
The constant time O(1)
means that no matter the size of the queue (it can have 10 or 1 million items): the enqueue, dequeue, peek and length operations must be performed at relatively the same time.
Let's look at a possible implementation of the queue data structure while maintaining the requirement that all operations must perform in constant time O(1)
.
const queue = new Queue()
is how you create an instance of a queue.
Calling queue.enqueue(7)
method enqueues the item 7
into the queue.
queue.dequeue()
dequeues a head item from the queue, while queue.peek()
just peeks the item at the head.
Finally, queue.length
shows how many items are still in the queue.
Regarding the implementation: inside the Queue
class the plain object this.items
keeps the items of the queue by a numerical index. The index of the head item is tracked by this.headIndex
, and the tail item is tracked by this.tailIndex
.
Queue** methods complexity**
queue()
, dequeue()
, peek()
and length()
methods of the Queue
class use only:
Property accessors (e.g. this.items[this.headIndex]
),
Or perform aritmetical operations (e.g. this.headIndex++
)
Thus the time complexity of these methods is constant time O(1)
.
The queue data structure is a type of First Input First Output (FIFO): the earliest enqueued item is the earlies to dequeue.
The queue has 2 main operations: enqueue and dequeue. Additionally, queues can have helper operations like peek and length.
All queue operations have to be performed in constant time O(1)
.