Queues

Data Structures

 

●    Understand the concepts of LIFO (Last In First Out) and FIFO (First In First Out) with regard to adding and removing data stored in a data structure.

●    Identify and describe different types of data structures that use the FIFO and LIFO concepts including stacks and queues.

 

Introduction

 

The bits that flow within a computer architecture constantly changing and therefore need to be allocated temporary memory within the limits of what is available (System RAM). Applications that are ‘greedy’ with memory can cause the computer system to be slow. The term memory leak refers to software that is taking too much temporary memory because it is not ‘cleaning up’ or removing the data in its structures that it no longer requires.

 

There are many different strategies a programmer may use to manage the data that is being used in any particular application. These structures include stacks, queues, arrays, lists, dictionaries, trees and collections amongst others and are particular to the programming language being used.

 

This document focuses particularly on two structural concepts - LIFO and FIFO:

 

●   LIFO (Last In First Out) – the last data added is first to be removed.

●   FIFO (First In First Out) – the first data added is first to be removed.

 

Two examples of data structures that use the LIFO and FIFO concept are:

 

●   Stacks – an example of a LIFO (Last In First Out).

●   Queues – an example of a FIFO (First In First Out).

 

 

 

 

1. Stacks

 

A stack is a LIFO (Last In First Out) data structure; the last data item to be added is the first to be removed.

 

●   Adding a new item of data to a stack is called pushing.

●   Removing a data item from the stack is called popping.

 




Add > Melon (Push)

Melon

Remove > Melon (Pop)


Pear



Orange



Apple



When new data is pushed onto a stack it gets put on the top of the stack – when data is popped is also taken from the top of the stack. So the last item of data placed on the stack is also the first item of data to be removed (Last In First Out).


If you were looking through a long list of items you may use your pencil or a ruler to keep track of your position in the list. The computer uses a similar method called a pointer.

 

Each data stack has a pointer called a head pointer indicating the current position of the top data item. In the example below the head pointer is currently set at data item 4 because this was the last item added to the stack.

 




Head Pointer >

Melon



Pear



Orange



Apple



If there is no space left on the stack to push data then a ‘stack overflow’ error may occur. Situations where a stack maybe used:

●   All applications use a call stack which manages what instructions are currently being processed (what the user is currently doing). When the instructions are completed they are removed from the stack

●   Any process that may require a reverse operation such as un-doing or re-doing a task in an application.

 

2. Queues

 

A queue is a FIFO (First In First Out) data structure; the first data item to be added is the first to be removed. An example of a queue in computer architecture would be a list of documents waiting to be printed (spooled). The documents would be printed in the order they were sent to the printer. Once printed the document would be removed from the queue.

 




Add > Document 4

Document 4



Document 3



Document 2



Document 1

Remove > Document 1


To keep track of both the first item and last data item added to the queue there are two stack pointers. In the example below the Head Pointer is Document 1 (First data item added), and the Tail Pointer is Document 4 (Last data item added).

 





Document 4

< Tail Pointer


Document 3



Document 2


Head Pointer >

Document 1



Situations that queue maybe used:

 

●   Data sent from the keyboard (Key strokes) – the key strokes will need to be temporarily stored whilst it decided what to do with it – i.e. maybe display it on the screen. If the keystrokes were not stored in order your words would appear jumbled up

●   Data sent as requests over a network – if your connection was temporarily disrupted then you


would want the tasks to resume where it left off

●   Any type of messaging system may queue messages

 

Circular Queues

 

In the printer document queue example above you may have wondered what happens to the remaining queue data when data is removed. Does it all move down like queue of people waiting at the shop checkout?

 

The computer could move all the data to occupy the space left by data that is removed, but this can be time consuming and a waste of resources. In practice the data does not move, only the queue front pointer and back pointer move as shown below.

 




Add > Document 4

Document 4

< Tail Pointer


Document 3


Head Pointer >

Document 2




Remove > Document 1


When the queue back pointer gets to the end of the queue it will go to the beginning of the queue to start filling the space that been made available by data that has been removed like so:

 


Document 5



Document 4



Document 3


Head Pointer >

Document 2


Add > Document 6

Document 6

< Tail Pointer


This pattern of circular iteration gives this type of queue the name circular queue. The maximum size of the queue will be determined by the programmer. The programmer will decide whether the end of the queue ‘overwrites’ the first items when it is full, or simply ignores incoming data and sends an error message.