UNIT III-Double Ended Queue

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

21CSC201J_Data Structures And Algorithms

QUEUES
• A queue is a data structure that models/enforces the first-
come first-serve order, or equivalently the first-in first-out
(FIFO) order.

• A waiting line is a good real-life example of a queue. (In fact,


the British word for “line” is “queue”.)
DEQUES (Double Ended Queue)
• Elements can be added or removed at either ends.
• It is a generalized version of the linear queue in which both
the insertion and deletion can be performed at both the ends.
Properties of Deque
• A Deque can perform both the Insertion and Deletion based
on the Last in First Out (LIFO) principle.
Properties of Deque
• A Deque can also perform the Insertion and Deletion based
on First in First Out (FIFO) principle.
Types of Deque

❑ Input restricted Deque

❑ Output restricted Deque


Input restricted Deque

❑ Input restricted Deque


• It is Deque that has certain restrictions for inserting an element.
• Elements can be inserted from one end.
• Elements can be removed at both the ends.
Output restricted Deque

Output restricted Deque


• It is Deque that has certain restrictions for removing an element
from deque.
• Elements can be removed from one end.
• Elements can be inserted at both the ends.
Operations in Deque

• The basic operations that can be performed on Deque are


1. Insertion at front
2. Insertion at rear
3. Deletion at front
4. Deletion at rear
Insertion at Front
Routine to insert an element at Front end
void Insert_Front ( int X, DEQUE DQ )
{
if( Front = = 0 )
Error(“Element present in Front!!!!! Insertion not possible”);
else if(Front = = -1)
{
Front = Front + 1;
Rear = Rear + 1;
DQ[Front] = X;
}
else
{
Front = Front - 1;
DQ[Front] = X;
}
}
Insertion at Rear
Routine to insert an element at Rear end
void Insert_Rear (int X, DEQUE DQ)
{
if( Rear = = Arraysize - 1)
Error(“Full Queue!!!! Insertion not possible”);
else if( Rear = = -1)
{
Front = Front + 1;
Rear = Rear + 1;
DQ[ Rear ] = X;
}
else
{
Rear = Rear + 1;
DQ[ Rear ] = X;
}
}
Delete at front
Routine to delete an element from Front end

void Delete_Front(DEQUE DQ)


{
if(Front = = - 1)
Error(“Empty queue!!!! Deletion not possible”);
else if( Front = = Rear )
{
X = DQ[ Front];
Front = - 1;
Rear = - 1;
}
else
{
X = DQ [ Front ];
Front = Front + 1;
}
}
Delete at rear
Routine to delete an element from Rear end

void Delete_Rear(DEQUE DQ)


{
if( Rear = = - 1)
Error(“Empty queue!!!! Deletion not possible”);
else if( Front = = Rear )
{
X = DQ[ Rear ];
Front = - 1;
Rear = - 1;
}
else
{
X = DQ[ Rear ];
Rear = Rear - 1;
}
}
APPLICATIONS OF DEQUE

Palindrome-checker
APPLICATIONS OF DEQUE

A-Steal job scheduling algorithm


• The A-Steal algorithm implements task scheduling for several
processors(multiprocessor scheduling).
• The processor gets the first element from the deque.

• When one of the processor completes execution of its own threads it can
steal a thread from another processor.
• It gets the last element from the deque of another processor and executes
it.
OTHER APPLICATIONS OF DEQUE

• Undo-Redo operations in Software applications.


• Storing a web browser's history. Recently visited URLs are
added to the front of the deque, and the URL at the back of the
deque is removed after some specified number of insertions at
the front.

You might also like