0% found this document useful (0 votes)
9 views

Queue– a Linear Data Structure

The document provides an overview of queues as a linear data structure, detailing their operations, including addition and deletion, and introducing concepts like circular queues and double-ended queues (deques). It explains the FIFO principle, the challenges of linear queues, and how circular queues address these issues. Additionally, it discusses priority queues and their applications in various fields such as customer service and processor scheduling.

Uploaded by

Shreya Painter
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Queue– a Linear Data Structure

The document provides an overview of queues as a linear data structure, detailing their operations, including addition and deletion, and introducing concepts like circular queues and double-ended queues (deques). It explains the FIFO principle, the challenges of linear queues, and how circular queues address these issues. Additionally, it discusses priority queues and their applications in various fields such as customer service and processor scheduling.

Uploaded by

Shreya Painter
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Queues

March 20, 2025 Data Structures and Algorithm


Queue – A Linear Data
Structure
A queue is non-primitive linear
data structure.
Closely related to Stacks.
It is homogenous collection of
elements.
Like a line
◦ In Britain people don’t “get in line”
they “queue up”.
e.g. railway reservation booth
customer queue
March 20, 2025 Data Structures and Algorithm 2
In queue new elements are added at
one end called the Rear end and the
existing elements are deleted from
other end called the Front end
On insertion of element rear
becomes
rear = rear +1
On deletion of element front
becomes
front = front + 1
It is also called First in First out (FIFO)
data structure.
It is also called First come first serve
(FCFS)
Queue

March 20, 2025 Data Structures and Algorithm


Operations on Linear
Queue
Addition of element
Deletion of element
Traverse
Working of queue

March 20, 2025 Data Structures and Algorithm 5


Algorithm for Addition in a
Queue
Qinsert (q[max],item)
Let q[max] is an array for implementing the queue
and
item is the element to be inserted into the queue
Set Front = -1 and Rear = -1
1. [Check overflow condition]
if Rear = MAXSIZE – 1 then
print, Queue Overflow and return.
else
2. set Rear = Rear + 1.
3. if Front < 0 then
set Front = Front + 1
end if
4. Queue[Rear] = item.
5. end if
6. Return

March 20, 2025 Data Structures and Algorithm 6


Algorithm for Deletion in a
Queue
Qdelete (q[max])
This algorithm deletes an item at the front of the
q[max]
1. [Check underflow condition]
if Front < 0
print Queue is empty and return.
2. else print deleted element is queue[Front]
3. if (Front = Rear)
set Front = -1, Rear = -1
else
Front = Front + 1
end if
4. End if
5.Return

March 20, 2025 Data Structures and Algorithm 7


Algorithm to traverse a
Queue
Qtraverse (q[max])
This algorithm traverse a queue q[max]
1. [Check underflow condition]
if Rear < 0
print Queue is empty and return.
else
2. Set i=Front
3. Repeat step 4 until i<=Rear
4. print Queue[i] , set i = i+1
5. Return
Problem with linear queue
 When a new item is inserted at the rear,
the pointer to rear moves upwards.
 Similarly, when an item is deleted from
the queue the front arrow moves
upwards.
 After a few insert and delete operations
the rear might reach the end of the
queue and no more items can be
inserted although the items from the
front of the queue have been deleted
and there is space in the queue.

March 20, 2025 Data Structures and Algorithm


Circular queue
To solve this problem, queues
implement wrapping around. Such
queues are called Circular Queues.
Is one in which the insertion of a
new element is done at the very
first location of the queue if the
last location of the queue is full.
Is one in which the first element
come just after the last.

March 20, 2025 Data Structures and Algorithm


It can be viewed as a mesh or
loop of wire, in which the two
ends are connected together.
It can overcome the problem of
unutilized space in linear queue.
Front will always be pointing to
the first element.
If front=rear the queue will be
empty.
Working of Circular queue

March 20, 2025 Data Structures and Algorithm


If the queue is full and new
element is inserted from the rear
the value of rear=(rear+1)%max
Queue[rear]=item
Similarly if element deleted from
queue than the value of
front=(front+1)%max

March 20, 2025 Data Structures and Algorithm


Algorithm for Addition in a Circular
Queue
CQinsert (q[max],item)
Let q[max] is an array for implementing the circular queue and
item is the element to be inserted into the circular queue
Set Front = -1 and Rear = -1
1. [Check overflow condition]
if Front = (Rear+1)%max then
print, Queue Overflow and return.
else
2. If (Front = -1) then
set Front=Front + 1
set Rear=Rear + 1
3. else
set Rear = (Rear+1)%max
end if
4. Queue[Rear] = item.
5. end if
6. Return

March 20, 2025 Data Structures and Algorithm 13


Algorithm for Deletion in Circular
Queue
CQdelete (q[max])
This algorithm deletes an item at the front of the q[max]
1. [Check underflow condition]
if Front < 0
print Queue is empty and return.
2. else print deleted element is queue[Front]
3. if (Front = Rear)
set Front = -1, Rear = -1
else
Front = (Front + 1)%max
end if
4. End if
5. Return

March 20, 2025 Data Structures and Algorithm 14


Algorithm to traverse circular
queue
CQtraverse (q[max])
This algorithm traverse a queue q[max]
1. [Check underflow condition]
if Front < 0
print Queue is empty and return.
else
2. If Front>Rear then
for i = front to max-1
print q[i]
for i = 0 to Rear
print q[i]
else
for i=Front to Rear
print q[i]
end if
3. End if
4.Return
Double Ended
Queue(deque)
It is also homogenous list of
elements in which insertion and
deletion operation performed both
the ends.
It is a double-ended queue and
commonly referred as deque.
There are two types of deque:
1. Input restricted deque
2. Output restricted deque
E.g. policy-based application (e.g.
low priority go to the end, high go
to the front)
March 20, 2025 Data Structures and Algorithm
Working of deque
Working of Input Restricted
Deque
Working of Output Restricted
Deque
Priority Queues
It is a collection of elements such
that each element has been assigned
a priority.
The order in which elements are
deleted and processed comes from
the following rules:
◦ An element of higher priority element
processed before of lower priority.
◦ Two elements having same priority are
processed according to the order in
which they were added to the queue.
More specialized data structure.
Used in multitasking operating
system. March 20, 2025 Data Structures and Algorithm
Types of priority queue
There can be two types:
◦ Ascending priority queue
◦ Descending priority queue
An ascending is a collection of items
into which items can be inserted
randomly and from which only the
smallest item can be removed.
In descending only the largest item
is deleted.
Working of priority queue

March 20, 2025 Data Structures and Algorithm


Application of Queue
• All kind of customer services(like
railway ticket reservation, movie
ticket reservation etc.)
• Access to shared resources (e.g.,
printer)
• Multiprogramming
• Processor scheduling (round robin
technique)

March 20, 2025 Data Structures and Algorithm 20

You might also like