The document provides an overview of various types of queues in data structures, including linear queues, circular queues, and priority queues. It details the insertion and deletion operations for each type, emphasizing the FIFO principle for standard and circular queues, as well as the priority assignment in priority queues. Additionally, it covers double-ended queues (deques) which allow insertion and deletion from both ends.
Queue
• Queue isLinear Data Structure
• It follows First In First Out(FIFO) principal
• It has two pointers front and rear
e.g.:
10 20 30 40 50
A[0] A[1] A[2] A[3] A[4]
Insertion
(ENQUEUE)
Deletion
(DEQUEUE)
3.
Operations on Queue
Insertion:
Algorithm:
Step1: If REAR = MAX – 1 then
Write “Queue is Overflow”
Goto step 4
[End of IF]
Step 2: IF FRONT=-1 and REAR=-1
SET FRONT=REAR=0
ELSE
SET REAR=REAR+1
[END OF IF]
Step 3: SET QUEUE [REAR] = NUM
Step 4: EXIT
Why Circular Queueis needed?
• Problem:
– Wastage of memory in standard queue in DEQUEUE
operation
10.
What is CircularQueue?
• The Arrangement of the elements Q[0], Q[1],
...,Q[n] in a circular fashion with Q[1]
following Q[n] is called Circular Queue.
• The last node is connected to first node to
make a circle.
• Initially, Both Front and Rear pointers points
to the beginning of the array.
• It is also known as “Ring Buffer”.
Operations on CircularQueue
Insertion:
Algorithm:
Step 1: IF FRONT=0 and REAR=MAX-1 Then
Write “Overflow”
Goto Step 4
[END OF IF]
Step 2: IF FRONT = 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
Operations on CircularQueue
Deletion:
Algorithm:
Step 1: If FRONT = -1 then
Write ("Circular Queue Underflow“)
GOTO Step 4
Step 2: SET VAL=QUEUE[FRONT]
Step 3: If FRONT = REAR then
SET FRONT=REAR=-1
ELSE
IF FRONT=MAX-1
SET FRONT=0
ELSE
SET FRONT=FRONT+1
[END OF IF]
[END OF IF]
Step 4: EXIT
Priority Queue
• Inpriority queue, each element is assigned a
priority.
• Priority of an element determines the order in
which the elements will be processed.
• Rules:
1. An element with higher priority will processed
before an element with a lower priority.
2. Two elements with the same priority are
processed on a First Come First Serve basis.
24.
Types of PriorityQueue
1. Ascending Priority Queue
In this type of priority queue, elements can be
inserted into any order but only the smallest
element can be removed.
2. Descending Priority Queue
In this type of priority queue, elements can be
inserted into any order but only the largest element
can be removed.
25.
Array Representation ofPriority Queue
Insertion Operation:
• While inserting elements in priority queue we
will add it at the appropriate position
depending on its priority
• It is inserted in such a way that the elements
are always ordered either in Ascending or
descending sequence
Deque / DoubleEnded Queue
• A list in which elements can be inserted or
deleted either end
• It is also known as “Head-Tail Linked List”
• It has two pointers LEFT and RIGHT, which
point to either end of the deque.
• Dequeue can be implemented using Circular
Array or a Circular doubly linked list where
Dequeue[n-1] is followed by Dequeue[0]
31.
Types of Deque
•Input Restricted Deque:-
In this dequeue, insertion can be done only at one of
the end, while deletion can be done from both ends.
• Output Restricted Deque:-
In this dequeue, deletion can be done only at
one of the ends, while insertions can be done
on both ends