DS-Unit2 - Queues
DS-Unit2 - Queues
Contents
Introduction to Queues
Circular Queues
Queue Real Examples
People standing in a queue Cars lined at a toll bridge
Even though there is space available, the overflow condition still exists
because the condition rear = MAX – 1 still holds true.
This is a major drawback of a linear queue.
Circular Queue- Insert
Algorithm
Step 1: IF ((FRONT = 0 and Rear = MAX – 1 ) || (Front==Rear -1 ))
◦ Write OVERFLOW, Goto step 4
◦ [END OF IF]
Step 4: EXIT
Priority Queue
Both the stack and the queue data structures the
elements are ordered based on the sequence in
which they are inserted.
In priority queue, the operations are based on the
priority of the element.
Example- Priority queues are widely used in
operating systems to execute the highest priority
process first.
An element with higher priority is processed before
an element with a lower priority.
Two elements with same priority are processed in
First come first serve (FCFS) basis.
Types of priority
queue
Types of priority queue
1. ascending priority queue
2. descending priority queue
An ascending priority queue is a collections of items into
which elements can be inserted arbitrarily and from which
only the smallest item can be removed.
Operations-
apq – ascending PQ.
pqinsert(apq, x)- inserts item x into ascending PQ.
pqmindelete(apq)- removes the smallest element from the
apq and returns its value.
Ascending Order Priority Queue
An ascending order priority queue gives the highest priority to the lower number in that
queue. For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20.
Firstly, you will arrange these numbers in ascending order. The new list is as follows: 4, 8,
12, 20. 35, 45. In this list, 4 is the smallest number. Hence, the ascending order priority
In the above table, 4 has the highest priority, and 45 has the lowest priority.
4 8 12 20 35 45
Types of priority
queue
An descending priority queue is a collections of items
into which elements can be inserted arbitrarily and
from which only the largest item will be removed.
Operations-
dpq – descending PQ.
pqinsert(dpq, x)- inserts item x into descending PQ.
pqmaxdelete(apq)- removes the largest element from
the dpq and returns its value.
Isempty()- determines if PQ is empty. Returns true if
empty otherwise returns false.
Descending Order Priority Queue
A descending order priority queue gives the highest priority to the highest
number in that queue. For example, you have six numbers in the priority
queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these
numbers in ascending order. The new list is as follows: 45, 35, 20, 12, 8,
In the above table, 4 has the lowest priority, and 45 has the highest
priority.
45 35 20 12 8 4
Array implementation
of Priority Queue
In stack and queue are implemented using an array such
that insertion and deletion involves accessing only one
element of an array. However, this is not possible in
priority queue.
Suppose that n elements of the priority queue pq are
maintained in positions 0 to n-1 of an array of size
maxpq, and suppose rear equals to the first empty
position, n. Then pqinsert (pq, x) is straightforward pqinsert(int pq[], int x)
operation: {
if (rear >= maxpq)
{
printf(“ priority queue
overflow”);
exit(1);
}
pq[rear]=x;
rear++;
}
Array implementation
of Priority Queue
If we want to attempt operation pqmindelete(pq) on
a ascending priority queue.
This raises two issues:
First, to locate smallest element, every element of
the array from items pq[0] to pq[rear-1] must be
examined.
Second, how to delete the element from the middle
of the array.