Unit - III Introduction To Queue and Operations of Queue ADT
Unit - III Introduction To Queue and Operations of Queue ADT
21CSC201J
DATA STRUCTURES AND ALGORITHMS
UNIT-3
Topic : Introduction to Queue, Operations on Queue ADT - Create, Enqueue and
Dequeue
Introduction to Queue
• Queue is a linear data structure in which the insertion and deletion operations are performed
at two different ends.
• In queue data structure, the insertion and deletion operations are performed based on FIFO
(First In First Out) principle.
• The insertion is performed at one end and deletion is performed at another end.
• In a queue data structure, the insertion operation is performed at a position which is known
as 'rear' and the deletion operation is performed at a position which is known as 'front'.
Introduction to Queue
• The representation of the queue is shown in the below image -
Introduction to Queue
Let us explain the concept of queues using the analogies given below.
• People moving on an escalator. The people who got on the escalator first
will be the first one to step out of it.
• People waiting for a bus. The first person standing in the line will be the
first one to get into the bus.
• People standing outside the ticketing window of a cinema hall. The first
person in the line will get the ticket first and thus will be the first one to
move out of it.
• Luggage kept on conveyor belts. The bag which was placed first will be the
first to come out at the other end.
• Cars lined at a toll bridge. The first car to reach the bridge will be the first
to leave.
Introduction to Queue
• Conceptual View of a Queue
Types of Queue
Types of Queue
Simple Queue or Linear Queue
• In Linear Queue, an insertion takes place from one end while the deletion occurs from
another end.
• The end at which the insertion takes place is known as the rear end, and the end at
which the deletion takes place is known as front end. It strictly follows the FIFO rule.
• The major drawback of using a linear Queue is that insertion is done only from the
rear end.
• If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue.
• In this case, the linear Queue shows the overflow condition as the rear is pointing to
the last element of the Queue.
Types of Queue
Circular Queue
• In Circular Queue, all the nodes are represented as circular.
• It is similar to the linear Queue except that the last element of the queue is
connected to the first element.
• It is also known as Ring Buffer, as all the ends are connected to another end.
• The representation of circular queue is shown in the below image -
• The drawback that occurs in a linear queue is overcome by using the circular
queue.
• If the empty space is available in a circular queue, the new element can be added
in an empty space by simply incrementing the value of rear.
• The main advantage of using the circular queue is better memory utilization.
Types of Queue
Priority Queue
• It is a special type of queue in which the elements are arranged based on the
priority.
• It is a special type of queue data structure in which every element has a priority
associated with it. Suppose some elements occur with the same priority, they
will be arranged according to the FIFO principle.
• The representation of priority queue is shown in the below image -
Types of Queue
Priority Queue
• Insertion in priority queue takes place based on the arrival, while deletion in
the priority queue occurs based on the priority. Priority queue is mainly
used to implement the CPU scheduling algorithms.
• There are two types of priority queue that are discussed as follows -
• Ascending priority queue - In ascending priority queue, elements can be
inserted in arbitrary order, but only smallest can be deleted first. Suppose
an array with elements 7, 5, and 3 in the same order, so, insertion can be
done with the same sequence, but the order of deleting the elements is 3,
5, 7.
• Descending priority queue - In descending priority queue, elements can
be inserted in arbitrary order, but only the largest element can be deleted
first. Suppose an array with elements 7, 3, and 5 in the same order, so,
insertion can be done with the same sequence, but the order of deleting the
elements is 7, 5, 3.
Types of Queue
Deque (or, Double Ended Queue)
• In Deque or Double Ended Queue, insertion and deletion can be done from both ends
of the queue either from the front or rear.
• It means that we can insert and delete elements from both front and rear ends of the
queue.
• Deque can be used as a palindrome checker means that if we read the string from both
ends, then the string would be the same.
• Deque can be used both as stack and queue as it allows the insertion and deletion
operations on both ends.
• Deque can be considered as stack because stack follows the LIFO (Last In First Out)
principle in which insertion and deletion both can be performed only from one end.
• The representation of the deque is shown in the below image -
Operations on a Queue
Enqueue :
• Step 1: Check if the queue is full or not by comparing the number of elements in the
queue with the maximum size of the queue.
• Step 2: If the queue is full, then display an overflow message and return.
• Step 3: If the queue is not full, then increment the rear pointer and insert the new
element at the position pointed by the rear pointer.
Dequeue :
• Step 1: Check if the queue is empty or not by comparing the number of elements in
the queue with 0.
• Step 2: If the queue is empty, then display an underflow message and end the
program.
• Step 3: If the queue is not empty, then remove the element at the front of the queue
and increment the front pointer.
Ways to implement the Queue ADT