Unit 3 1
Unit 3 1
Introduction
• Queue is an important data structure which stores its elements in
an ordered manner.
• We can explain the concept of queues using the following analogy:
People moving on an escalator. The people who got on the escalator
first will be the first one to step out of it.
• A queue is a FIFO (First-In, First-Out) data structure in which the
element that is inserted first is the first one to be taken out.
• The elements in a queue are added at one end called the rear and
removed from the other one end called the front.
INSERTING AN ELEMENT
DELETING A ELEMENT
Array
Representation
of
Queues
Array Representation of Queues
• Queues can be easily represented using linear arrays.
• Every queue has front and rear variables that point to the position
from where deletions and insertions can be done, respectively.
• Consider the queue shown in figure
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Array Representation of Queues
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
[END OF IF]
Step 2: Exit
LINKED LIST
Representation
of
Queues
Linked Representation of Queues
• In a linked queue, every element has two parts: one that stores data
and the other that stores the address of the next element.
• The START pointer of the linked list is used as FRONT.
• We will also use another pointer called REAR which will store the
address of the last element in the queue.
• All insertions will be done at the rear end and all the deletions will
be done at the front end.
• If FRONT = REAR = NULL, then it indicates that the queue is empty.
1 7 3 4 2 6 5 X
FRONT REAR
Inserting an Element in a Linked Queue
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR = PTR
SET FRONT->NEXT = REAR->NEXT = NULL
ELSE
SET REAR->NEXT = PTR
SET REAR = PTR
SET REAR->NEXT = NULL
[END OF IF]
Step 4: END
Deleting an Element from a Linked Queue
0 1 2 3 4 5 6 7 8 9
90 49 7 18 14 36 45 21 99 72
front=0 1 2 3 4 5 6 7 8 rear = 9
If rear != MAX – 1, then the rear will be incremented and value will
be inserted
90 49 7 18 14 36 45 21 99
front=0 1 2 3 4 5 6 7 rear= 8 9
front=1 2 3 4 5 6 7 8 rear= 9
Algorithm to Insert an Element in a
Circular Queue
Step 1: IF FRONT = 0 and Rear = MAX – 1, then
Write “OVERFLOW”
Goto Step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1, then;
SET FRONT = REAR = 0
ELSE IF REAR = MAX – 1 and FRONT != 0
SET REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: Exit
Deleting an Element from a Circular Queue
• To delete an element again we will check for three conditions:
If front = -1, then it means there are no elements in the queue. So
an underflow condition will be reported.
0 1 2 3 4 5 6 7 8 9
If the queue is not empty and after returning the value on front, if
front = rear, then it means now the queue has become empty and
so front and rear are set to -1.
Delete this element and set
81
rear = front = -1
0 1 2 3 4 5 6 7 8 front=rear= 9
If the queue is not empty and after returning the value on front, if
front = MAX -1, then front is set to 0.
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8 front= 9
Algorithm to Delete an Element from a Circular Queue
29 37 45 54 63
0 1 2 LEFT = 3 4 5 6 RIGHT = 7 8 9
63 27 18
42
RIGHT = 0 1 2 3 4 5 6 LEFT = 7 8 9
Priority Queues
• A priority queue is a queue in which each element is assigned a
priority.
• The priority of elements is used to determine the order in which
these elements will be processed.
• The general rule of processing elements of a priority queue can be
given as:
An element with higher priority is processed before an element
with lower priority
Two elements with same priority are processed on a first come
first served (FCFS) basis
• Priority queues are widely used in operating systems to execute the
highest priority process first.
• In computer’s memory priority queues can be represented using
arrays or linked lists.
Array Representation of Priority Queues
A 1 B 2 C 3 D 3 E 4 X
Queue A Queue B
Applications of Queues
• Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
• Queues are used to transfer data asynchronously e.g., pipes, file IO,
sockets.
• Queues are used as buffers on MP3 players and portable CD players,
iPod playlist.
• Queues are used in Playlist for jukebox to add songs to the end, play
from the front of the list.
• Queues are used in OS for handling interrupts. When programming a
real-time system that can be interrupted, for ex, by a mouse click, it
is necessary to process the interrupts immediately before
proceeding with the current job. If the interrupts have to be handled
in the order of arrival, then a FIFO queue is the appropriate data
structure