Professional Documents
Culture Documents
Queues
Session II
1
Data Structures & Algorithms
• Queue
• Application of Queue
• Types of Queue
• Basic Operations of Queue
2
Data Structures & Algorithms
Queue
• Queue is defined as a waiting line for a specific task,
For e.g.,
People waiting to purchase tickets
The first person in line is the first person served.
3
Data Structures & Algorithms
Application of Queue
• A line in a bank
Types of Queues
• Circular Queue
• Deque
• Priority Queue
4
Data Structures & Algorithms
Circular Queue
• This technique essentially allows the queue to wrap around upon reaching
the end of the array.
5
Data Structures & Algorithms
Dequeue
• Elements can be added or removed at either end but not in middle
(deque means double ended queue).
• Deque with pointers Left and Right will point to the two ends of the
deque.
• Types of Deque:
– Input restricted deque - which allows insertion at only one end of
the list but allows deletion at both ends of the list.
– Output restricted deque - which allows deletion at only one end of
the list but allows insertion at both ends of the list.
6
Data Structures & Algorithms
Priority Queue
Def - A queue which holds items that are sorted such that the highest priority
Item is always the next one to be retrieved.
• A Priority Queue is a collection of elements
• Each element has been assigned a priority and based on the order in
which elements are deleted and processed from the rules specified
below:
– A higher priority element is processed before any lower priority element.
– Any two elements having same priority will be accessed according to the order
in which they are added in the queue.
7
Data Structures & Algorithms
Basic Queue Operations
• Remove (q, x)- delete the element from the queue q and set x to its
contents. This operation can be applied only if the
queue is not empty.
8
Data Structures & Algorithms
Algorithm for Insert an Element in Queue
Algorithm insertq
Procedure insertq [ Q, F, R, N, Y]
// F and R pointers to the front and rear elements of a queue Q
// Consisting of N elements, and an element Y,
// this procedure insert Y at the rear of the queue.
1. [Overflow?]
if R >= N then
write( “overflow” )
return
2. [ Increment rear pointer]
R =R+1
3. [ Insert element]
Q[R] = Y
4. [Is Front Pointer properly set]
IF F=0 Then
F=1
return
9
Data Structures & Algorithms
Algorithm for Delete an Element in Queue
Algorithm removeq
Procedure removeq [ Q, F, R]
// F and R pointers to the front and rear elements of a queue Q
// to which they correspond, this function deletes and
// returns the last element of the queue
// Y is the temporary variable.
1. [Underflow ?]
if F= 0 then
write( “Underflow” )
return(0) [0 denotes an empty queue]
2. [ Delete an element ]
Y =Q[F]
3. [ Queue empty ?]
if F=R then
F=R=0
else
F=F+1 [Increment front pointer]
4. [Return element]
10
Return (Y)
Data Structures & Algorithms
{
If empty (&q)
{ write (“Queue is empty”);
return ture;
[queue is empty]
}
else
{ write (“Queue is not empty”);
return false;
[queue is not empty]
}
}
11