TMF1434 Lec07
TMF1434 Lec07
The first person to join a line is the first person to be served, or leave
the line
New person is added to a queue at the back
A person leaves a queue from the front
6 Queue Concept
A queue is a data structure
Suppose
To summarise,
queueFront gives the index of the first element of the
queue
queueRear gives the index of the last element of the queueFront changes
queue after each
deleteQueue
operation
To add an element to the queue:
1. Advance queueRear to the next array position
2. Add the element to the position that queueRear is queueRear changes
after each addQueue
pointing to operation
Circular queue
15 Implementation of Queues as Circular
Arrays (cont’d)
To insert an item
Advance the queue index queueRear
queueRear = (queueRear + 1) % maxQueueSize;
In the two
cases:
queueFront
and
Figure (b) represents a full queue queueRear
have same
values
19 Solution 1: Use variable count
This solution is very useful if the user of the queue frequently needs to
know the number of elements in the queue
20 Solution 2
Let queueFront indicates the index of array position preceding first
element of the queue
Rather than the index of the (actual) first element itself
Assume queueRear still indicates the index of last element
Queue is empty if queueFront == queueRear
Priority queues
Customers (jobs) with higher priority pushed to the front of the queue
Implementation
Ordinary linked list
Keeps items in order from the highest to lowest priority
Treelike structure
Very effective
(To be studied during “Sorting Algorithms”)
29 STL class priority_queue
Any
Question?