ivangeorgiev.dev

Introduction to data structures: Queue

Learn about the queue data structure in an intuitive way

November 18th, 2024 3 minute read

The queue data structure should be one of the more intuitive data structures. The reason is that we spend so much of our time waiting in lines (i.e. queues).
The queue data structure follows an interesting principle called "first in first out" or FIFO for short.
Let's learn what the queue data structure is and how it works.

We mentioned that we spend a lot of our time in queues. Let's give a few examples:

Queues are used when there are more items than can be handled (i.e. processed) and there is a need to store them and handle them at a later time.
In the example with the groceries there is a queue because there are more customers than there are employees working at the registers.
In the example with the venue, there is a queue because there are more people that want to get into the vanue than there are enterances to the venue.

What is the FIFO principle?
The FIFO principle states that the first item that was inserted into the queue is the first one to be removed from the queue.
In our example with the groceries, the first person that entered the queue (i.e. line) is the first person to pay for their groceries. Someone could of course "cut the line" and try to pay for their groceries before the people in front of them, but that is not very nice.
In programming "cutting the line" or removing items from positions other than the front of the queue is not allowed.

The queue data structure usually provides the following operations:

Let's give a few practical uses of the queue data structure:

How do queues work internally?
The queue data structure usually uses a linked list internally. It can also work with an array, but the linked list offers greater flexibility which goes well with the dynamic nature of queues (i.e. they change their size often).

If you enjoy my articles, please consider supporting me.