0% found this document useful (0 votes)
15 views22 pages

DS-Unit2 - Queues

Uploaded by

kamblisoham28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

DS-Unit2 - Queues

Uploaded by

kamblisoham28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Queues

Contents

Introduction to Queues
Circular Queues
Queue Real Examples
People standing in a queue Cars lined at a toll bridge

People moving on an escalator Luggage kept on conveyor belts


Queue
A queue is a FIFO (First-In, First-Out) data structure in which the element that is
inserted first is the first one to be taken out.
The elements in a queue are added at one end called the REAR and removed from
the other end called the FRONT.

Front Rear Rear Rear


Queue ADT

Queue Q, Front, Rear


Operations
1. Insert(Q, val) – Insert new element at the end of the queue.
2. Remove (Q)- Removes element from the front of the queue.
3. isEmpty(Q)- returns true if queue is empty else returns false
4. IsFull(Q)- returns true if queue is full else returns false
Insert operation
Step 1: IF REAR == MAX-1
Write OVERFLOW
Goto step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT
Remove operation
Step 1: IF FRONT == -1 OR FRONT > REAR
◦ Write UNDERFLOW
ELSE
◦ SET VAL = QUEUE[FRONT]
◦ SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Circular queue
Overcomes the disadvantage of linear queue

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 2: IF FRONT = -1 and REAR = -1


◦ SET FRONT = REAR = 0
◦ ELSE IF REAR = MAX - 1 and FRONT != 0
◦ SET REAR = 0
◦ ELSE
◦ SET REAR = REAR + 1
◦ [END OF IF]

Step 3: SET QUEUE[REAR] = VAL


Step 4: EXIT
Circular Queue- Delete
Algorithm
Step 1: IF FRONT = -1
◦ Write UNDERFLOW
◦ Goto Step 4
◦ [END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT == REAR


◦ SET FRONT = REAR = -1
◦ ELSE
◦ IF FRONT = MAX -1
◦ SET FRONT = 0
◦ ELSE
◦ SET FRONT = FRONT + 1
◦ [END of IF]
◦ [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

queue treats number 4 as the highest 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,

4. In this list, 45 is the highest number. Hence, the descending order

priority queue treats number 45 as the highest priority.

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.

insertpq (pq, 10)


6 5 2 18 7
Possible Solutions
Possible Solution to deal with empty location after
delete operation is
To mark the location as -1 to indicate that it is a
empty location and when rear reaches the maxpq,
then compact the array by and reset the rear.
For every insertion, find the first empty location
and insert the new item.
Compact the array on each deletion.
Maintain the elements in a ordered manner
(ascending order), then deletion will be simple
operation where only the first element is to be
removed, however insertion operation involves to
search for the position to insert into the ordered
array.
Priority queue
program
#define MAX 10
int pq[MAX], front=0, rear=0; rear=5
void insertpq(int pq[], int x)
{ int i; 1 2 4 5 6
if (rear>=MAX) Insertpq(pq, 3)
{ printf (“overflow”); exit (1);} 3 is inserted after 2
i=rear-1;
while(i>=0 && pq[i]> x) 1 2 3 4 5 6
{pq[i+1]=pq[i]; i--;} shift
pq[i+1]=x; rear++;
rear=6
}
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy