The Queue ADT

The queue is another abstract data structure. A physical representation of a queue is a line at the checkout of a shop or at the bank automatic teller machine. When you join the queue you go to the rear of the line. Customers, who have been served, move from the front of the queue. Like the stack, the queue usually holds things of the same data type. We usually draw queues in a horizontal direction. However, you can draw the queue vertically if you wish. The main property of the queue ADT is that elements are added to the rear of the queue and are removed from the front of the queue.

Here is the minimal set of operations that we would need for an abstract queue:

Create

Add or insert

Remove or delete

Is Empty

Destroy

Useful operations might be Count() and Display().

Queues are useful structures because they produce a certain order in which the contents of the queue are used. This ordering is known as First In First Out (FIFO). Since the queue is holding elements of the same type it should be quite simple to take this structure and implement it using linked nodes, as shown above. Using the linked node structure gives us a distinct advantage over the implementation of the queue using static arrays. We can easily increase the size of the list and we do not need to move elements up to the front of the queue. The node structure we will use will be the same as for the stack data structure. The node will hold some data and also a link / pointer to the next node in the chain.

Looking at the queue structure, we can see that the queue is created and destroyed in a similar manner to the stack data structure. We add and remove from the top of the stack, but we remove from the front of the queue and add at the rear. So that we can make use of the stack pop operation, we shall rename it remove. The create operation is similar to that of the stack; we just point to the null character and the destroy operation will be the same. We won't discuss these operations any further in relation to the queue data structure.

Next: Adding Nodes to a Queue