Types of Queue
There are four different types of
queues:
•Simple Queue
•Circular Queue
•Priority Queue
•Double Ended Queue
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Simple Queue
In a simple queue, insertion takes place at the rear and removal occurs at the
front. It strictly follows the FIFO (First in First out) rule.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Circular Queue
In a circular queue, the last element points to the first element making a circular
link.
The main advantage of a circular queue over a simple queue is better memory
utilization.
If the last position is full and the first position is empty, we can insert an
element in the first position.
This action is not possible in a simple queue.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Priority Queue
A priority queue is a special type of queue in which each element is associated
with a priority and is served according to its priority.
If elements with the same priority occur, they are served according to their order in
the queue.
Insertion occurs based on the arrival of th
values and removal occurs based on
priority.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Deque (Double Ended
Queue)
In a double ended queue, insertion and removal of elements can be performed
from either from the front or rear. Thus, it does not follow the FIFO (First In First
Out) rule.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Circular Queue
Circular Queue works by the process of circular increment i.e. when we try to
increment the pointer and we reach the end of the queue, we start from the
beginning of the queue.
Here, the circular increment is performed by modulo division with the queue size.
That is,
if REAR + 1 == 5 (overflow!), REAR = (REAR + 1)%5 = 0 (start of queue)
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Circular Queue Operations
The circular queue work as follows:
• two pointers FRONT and REAR
• FRONT track the first element of the queue
• REAR track the last elements of the queue
• initially, set value of FRONT and REAR to -1
1. Enqueue Operation
• check if the queue is full
• for the first element, set value of FRONT to 0
• Circularly increase the REAR index by 1 (i.e. if the rear reaches the end, next it would
be at the start of the queue)
• add the new element in the position pointed to by REAR
2. Dequeue Operation
• check if the queue is empty
• return the value pointed by FRONT
• circularly increase the FRONT index by 1
• for the last element, reset the values of FRONT and REAR to -1
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Circular Queue Operations
However, the check for full queue has a new additional
case:
•Case 1: FRONT = 0 && REAR == SIZE - 1
•Case 2: FRONT = REAR + 1
The second case happens when REAR starts from 0 due to
circular increment and when its value is just 1 less
than FRONT, the queue is full.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Circular Queue Complexity
Analysis
The complexity of the enqueue and dequeue operations of a circular queue
is O(1) for (array implementations).
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Application of Circular
Queue
• CPU scheduling
• Memory management
• Traffic Management
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Practice Examples
1. Program to find array size.
2. Write Program to check if two arrays are the same or not.[Asked in: Amazon,
Goldman Sachs]
3. Write Program to find Triplets with a given sum.
4. Write Program for basic array operations (Insert, delete and search an
element)
5. Write Program to find smallest and largest element in an array
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority and is served according to its priority. If elements
with the same priority occur, they are served according to their order in the
queue.
Generally, the value of the element itself is considered for assigning the
priority.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Priority Queue
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Priority Queue
Priority Queue Applications
Some of the applications of a priority queue are:
• Dijkstra's algorithm
• for implementing stack
• for load balancing and interrupt handling in an operating system
• for data compression in Huffman code
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
Deque or Double Ended Queue is a type of queue in which insertion and removal
of elements can be performed from either from the front or rear. Thus, it does not
follow FIFO rule (First In First Out).
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
Types of Deque
•Input Restricted Deque
In this deque, input is restricted at a single end but allows deletion at both the
ends.
•Output Restricted Deque
In this deque, output is restricted at a single end but allows insertion at both
the ends.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
Operations on a Deque
Before performing the following operations, these steps are followed.
1. Take an array (deque) of size n.
2. Set two pointers at the first position and set front = -1 and rear = 0.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
1. Insert at the Front
This operation adds an element at the front.
1.Check the position of front.
2. If front < 1, reinitialize front = n-1 (last index).
3. Else, decrease front by 1.
4. Add the new key 5 into array[front]
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
2. Insert at the Rear
This operation adds an element at the rear.
1.Check if the array is full.
2. If the deque is full, reinitialize rear = 0 .
3. Else, increase rear by 1.
4. Add the new key 5 into array[rear]
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
3. Delete from the Front
This operation deletes an element from the front.
1.Check if the deque is empty.
2. If the deque is empty (i.e. front = -1), deletion cannot be performed
(underflow condition).
3. If the deque has only one element (i.e. front = rear), set front = -1 and rear
= -1.
4. Else if front is at the end (i.e. front = n - 1), set go to the front front = 0.
5. Else, front = front + 1.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
4. Delete from the Rear
This operation deletes an element from the rear.
1.Check if the deque is empty.
2. If the deque is empty (i.e. front = -1), deletion cannot be performed
(underflow condition).
3. If the deque has only one element (i.e. front = rear), set front = -1 and rear
= -1.
4. Else if rear is at the front (i.e. rear = 0), set go to the front rear=n-1.
5. Else, rear = rear - 1.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Double Ended Queue
(Deque)
5. Check Empty
This operation checks if the deque is empty .
If front = -1, deque is empty.
6. Check Full
This operation checks if the deque is full.
If front = 0 and rear = n-1 OR front = rear+1, the deque is
full.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Deque Implementation.
Time Complexity
The time complexity of all the above operations is constant i.e. O(1).
Applications of Deque Data Structure
1.In undo operations on software.
2.To store history in browsers.
3.For implementing both stacks and queues.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video