- Read Tutorial
In the world of algorithms you won't make it far without a clear understanding of how the queue data structure works. In this guide to the queue data structure we'll walk through:
- What the queue data structure is
- What queues are used for
Lastly we'll finish up by taking a practical look at how queues can be used in real world development.
Abstract Data Structures
Even though a number of languages, including Python, Ruby and Scala, have a Queue class, typically queues are considered abstract data structures. This means that they are used to help explain algorithms and processes, but are rarely used in applications.
With that being said it is still important to learn how queues work, especially if you are going an algorithm course.
Queues In the Real World
Thankfully there is an easy way to understand how queues work in the real world, and to see them in action we get to take a trip to Disneyland.
The workflow for going on a ride at Disneyland is pretty straightforward.
- You get in line.
- One by one the individuals at the front of the line are allowed on the ride.
- You follow the line and eventually it's your turn and you get on the ride.
- When you're on the ride you are no longer in the line.
In this example the Disneyland line is the queue and you are an element in the queue. Much like our amusement park line, queues are made up of elements and have a strict set of rules that the elements need to follow.
Queue Data Flow
Looking at the data flow for this data structure, queues follow what's called a First In First Out (FIFO) type of data flow. This means that if you place the following items into the queue:
4, 2, 4, 6, 1, 2, 6
The first 4
will be the first element allowed to leave the queue because it was the first element added to the queue. This type of behavior differs from the Array
data structure that allows any element to be removed based on its index. Much like the Disneyland example, queues don't allow for cutting in line.
Queue Methods
In the study of algorithms, queues typically have two methods:
Enqueue
Dequeue
The enqueue
method adds items to the end of a queue data structure. And the dequeue
method removes the first element.
Practical Implementation
Let's see how the Python programming language works with the Queue data structure.
We're going to build a small batting order program that takes a list of baseball players and then runs through the lineup, just like in a real baseball game.
Creating a Queue Class
class NewQueue: def __init__(self): self.queue = [] def enqueue(self, item): return self.queue.append(item) def dequeue(self): return self.queue.pop(0)
This is actually of the behavior that we'll need for the queue to work properly. Let's walk through it by creating a new queue called batting_order:
batting_order = NewQueue()
This will create an empty queue that we can work with and store it in the batting_order
variable.
Using Enqueue
Now let's use the enqueue
method to add values to the queue.
We can add players one at a time like this:
batting_order.enqueue('Springer') batting_order.enqueue('Altuve') batting_order.enqueue('Bregman')
If you print out the batting order, you'll now see the full list of players:
['Springer', 'Altuve', 'Bregman']
Using Dequeue
With our batting order complete with three elements, what happens each time a player comes to the plate? That's where the dequeue process comes in. If we run the following code:
batting_order.dequeue()
Will return the value of Springer
because he was the first one we added to the batting_order
queue. If we run it again it will return Bregman
.
Summary of Queues
And that's it! The Queue data structure is quite straightforward, which is part of its strength. Whenever you hear someone referencing a queue you'll know that the data structure uses a First In First Out data flow and that it has two key methods: enqueue and dequeue.
What's Next?
In the next guide we'll walk through the Stack data structure.