A Queue is a FIFO (First In First Out) data structure. The first item
to be added is the first one to be removed. (Think of a queue at a
supermarket checkout!)
The implementation of a queue involves two
pointers:
- Front - which points to the front of the queue
(the item which has been in the queue longest).
- Back - which points to the back of the queue
(the item most recently added) The diagram shows a queue
after two names have been added :
8 |
|
Front
Pointer = 1
Back
Pointer = 2
|
7 |
|
6 |
|
5 |
|
4 |
|
3 |
|
2 |
Harry |
1 |
William |
If 'Jonathan' and
'Hayley' are then added to the queue:
8 |
|
Front
Pointer = 1
Back
Pointer = 4
|
7 |
|
6 |
|
5 |
|
4 |
Hayley |
3 |
Jonathan |
2 |
Harry |
1 |
William |
Then a name is removed and 'Paul'
is added....
8 |
|
Front
Pointer = 2
Back
Pointer = 5
|
7 |
|
6 |
|
5 |
Paul |
4 |
Hayley |
3 |
Jonathan |
2 |
Harry |
1 |
|
An example
of a queue in a computer system would be the printer queue ie
spooled documents waiting for output to a printer. The documents
would be printed in the order in which they were spooled.
Another example : In a multi-access
system jobs waiting for processing are
placed in a queue.
|