Professional Documents
Culture Documents
3
M. H. Bhuyan Lec 24 and 25
Representation of Queues
Queues may be represented in the computer, by means of one-way
list or linear array.
Each queue maintains two pointer variable: front and rear
When an element is deleted from the queue, the front value is
incremented by 1.
front = front + 1
When an element is inserted into the queue, the rear value is
incremented by 1.
rear= rear + 1
4
M. H. Bhuyan Lec 24 and 25
Example
QUEUE
AA BB CC DD
1 2 3 4 5 6 7 8
front = 1
rear = 4
QUEUE
BB CC DD
1 2 3 4 5 6 7 8
front = 2
rear = 4
5
M. H. Bhuyan Lec 24 and 25
Problems
1 2 3 4 5
a) Insert A, B, C
b) Delete A
c) Insert D, E
d) Delete B, C
e) Insert F
f) Delete D
6
M. H. Bhuyan Lec 24 and 25
Contd
g) Delete D
h) Insert G, H
i) Delete E
j) Delete F
k) Insert K
l) Delete G, H
m) Delete K
7
M. H. Bhuyan Lec 24 and 25
Queue Algorithms
Algorithm Q_INSERTION(QUEUE, N, front, rear, item)
Input: N number of item to be inserted into the QUEUE
Output: current status of the QUEUE
Step 1: if front=1 and rear=N, or if front=rear + 1 then
write: overflow and return
Step 2: if (front= NULL) then
set front = 1 and rear = 1
else if (rear =N) then
set rear =1
else
set rear=rear + 1
end if
Step 3: set QUEUE[rear] = item
Step 4: exit
8
M. H. Bhuyan Lec 24 and 25
Queue Algorithms
9
M. H. Bhuyan Lec 24 and 25
Queue Applications
Real life examples
Waiting in line
Waiting on hold for tech support
10
M. H. Bhuyan Lec 24 and 25
Dqueue
Dqueue is a linear list in which elements can be added or removed
at either end but not in the middle.
11
M. H. Bhuyan Lec 24 and 25
Priority Queue
A priority queue is a collection of elements such that each
element has been assigned a priority and such that the order in
which elements are deleted and processed comes from the
following rules:
An element of higher priority is processed before any element of
lower priority
Two elements with the same priority are processed according to the
order in which they were added to the queue.
Priority queue representation
One-way list
Array representation
12
M. H. Bhuyan Lec 24 and 25
Questions?
13
M. H. Bhuyan Lec 24 and 25