You are on page 1of 13

Queues

Department of Computer Science and Engineering


School of Engineering and Technology
Kaziranga University
Jorhat-785006, Assam.
monowar@kazirangauniversity.in
Queue

A queue is a linear list. Data can be inserted at one end (rear)


and deleted from the other end (front).

First In First Out - FIFO


2
M. H. Bhuyan Lec 24 and 25
Queue Operations
There are four basic queue operations.
Data can be inserted at the rear and deleted from the front.
Enqueue: inserts an element at the rear of the queue.
Dequeue: deletes an element at the front of the queue.
Queue Front: examines the element at the front of the queue.
Queue Rear: examines the element at the rear of the queue.

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

Consider a circular array QUEUE (initially empty) with N=5


memory locations, find the front and rear value.

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

Algorithm Q_DELETION(QUEUE, N, front, rear, item)


Input: N number of item to be deleted from the QUEUE
Output: current status of the QUEUE
Step 1: if front = NULL, then
write Underflow and return
Step 2: set item=QUEUE[front]
Step 3: if front=rear, then
set front = NULL and rear = NULL
else if front = N, then
set front=1
else
set front = front+1
end if
Step 4: Exit

9
M. H. Bhuyan Lec 24 and 25
Queue Applications
Real life examples
Waiting in line
Waiting on hold for tech support

Applications related to Computer Science


Threads
Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

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.

There are two variations in a dqueue


Input restricted dqueue: It is a dqueue which allows insertions at
only one end of the list but allows deletions at both ends of the list.
Output restricted dqueue: It is a dqueue which allows deletions at
only one end of the list but allows insertions at both ends of the list

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

You might also like