A queue is a fairly common data storage method. However, unlike many methods of storing data, where we keep the data for the entire runtime of a function or even the entire application, a queue’s data is designed to be consumable. That is, at some point data will enter the queue, and then it will later leave the queue.
In what order will it leave, well a queue works on a first in, first out basis, often referred to as FIFO. As you might imply from FIFO, a Queue is designed to work with data that is constantly being added (in) and removed (out).
A queue is designed so that when an element is entered into the queue, it waits its turn based upon when it entered. When one needs to be processed, the first piece of data entered, is the first one chosen to be used. All of the other elements move up one position, so the second one entered becomes the next element to be chosen, the third element becomes next in line, etc.
A major advantage of this is every data entered has a chance to be processed no matter how many new elements are added to the queue, because you are always moving through the queue.
Queues in Real Life
A simple example you’re probably used to is when you are standing in line for a ride at an amusement park. People get on the ride in the order that they enter the line. In fact, industry insiders refer to those lines as a queue.
When a queue is built with a linked list, it is very efficient and easy to use. Data elements will usually have a pointer to the next item in the list. That way you don’t have to move items through an array, or change their locations in memory.
Computer Queues
Queues are often used for messaging data between systems, making sure data is sent in a specific order so it maintains a level of integrity. Now, I cannot assume that the message will get there in the same order, but I can make sure it is sent in the right order, which improves the chances of it being received in the right order.
Queues can, and often, are designed so that they store a list of programs (or sub-programs) to run, messages to send, etc.
A queue can often be implemented by either an array, or a linked list style of data structure. While in our examples, we’ll point to something simple like an int, they can point to any type of data, including ones which we create, such as an employee, student, product, etc.
How Queues Work was originally found on Access 2 Learn