Types of Queues
Learn about the different types of queues and their practical applications.
We'll cover the following...
Overview
Let’s have a look at an application of the list structure to create a queue. A queue is a special kind of buffer, summarized as First In First Out (FIFO). The idea is to act as a temporary stash so one part of an application can write to the queue while another part consumes items from the queue.
A database might have a queue of data to be written to disk. When our application performs an update, the local cache version of the data is updated so all other applications can see the change. However, the write to the disk may be placed in a queue for a writer to deal with a few milliseconds later.
When we’re looking at files and directories, a queue can be a handy place to stash details of the directories so they can be processed later. We’ll often represent a directory as the path from the root of the filesystem to the file of interest. The algorithm works like this:
queue starts emptyAdd the base directory to the queueWhile the queue is not empty:Pop the first item from the queueIf the item is a file:Process the itemElse if the item is a directory:For each sub-item in the directory:Add this sub-item to the queue
We can visualize this list-like structure as growing via an append()
and shrinking via pop(0)
. It would look like this:
Implementation
The idea is for the queue to grow and shrink: each directory grows the queue, and each file shrinks the queue. Eventually, all the files and directories have been processed, and ...