0% found this document useful (0 votes)
16 views52 pages

Week5 Priority Queue

The document provides an overview of queues, detailing their properties, operations, and implementations, including circular queues and priority queues. It explains the First In First Out (FIFO) principle, various queue operations such as enqueue and dequeue, and the concept of job scheduling based on priority. Additionally, it covers the implementation details and applications of priority queues in computer science.

Uploaded by

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

Week5 Priority Queue

The document provides an overview of queues, detailing their properties, operations, and implementations, including circular queues and priority queues. It explains the First In First Out (FIFO) principle, various queue operations such as enqueue and dequeue, and the concept of job scheduling based on priority. Additionally, it covers the implementation details and applications of priority queues in computer science.

Uploaded by

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

Queues

Department of Computer Science and Software


Engineering
Capital University of Sciences & Technology,
Islamabad Pakistan
Queues

 Have same type of elements.


 Element can be added from one end. (back or rear)
 Element can be removed from other end. (front)
 First In First Out (FIFO)
Queues Operations

 initializeQueue: Initializes the queue to an empty state.


 isEmptyQueue: Determines whether the queue is empty. If the queue is
empty, it returns the value true; otherwise, it returns the value false.
 isFullQueue: Determines whether the queue is full. If the queue is full, it
returns the value true; otherwise, it returns the value false.
 Front: Returns the front, that is, the first element of the queue. Prior to this
operation, the queue must exist and must not be empty.
 Back: Returns the last element of the queue. Prior to this operation, the queue
must exist and must not be empty.
Queues Operations

 addQueue : Adds a new element to the rear of the queue. Prior to this
operation, the queue must exist and must not be full.
 deleteQueue: Removes the front element from the queue. Prior to this
operation, the queue must exist and must not be empty.
Queues as an array

ADD A

Empty Queue
Queues

Front = 0
Rear = 0

rear
A
front
Queue
Queues

Front = 0
Rear = 0
ADD C

rear
A
front
Queue
Queues

Front = 0
Rear = 1
rear
C
A
front

Queue
Queues

Front = 0
Rear = 1
rear
ADD E
C ADD F
A ADD G
front

Queue
Queues

rear
G Front = 0
F Rear = 4
E
C
A
front

Queue
Queues

rear
G Front = 0
F Rear = 4
E Delete
C
A
front

Queue
Queues

rear
G Front = 1
F Rear = 4
E
C
front A

Queue
Queues

rear
G Front = 1
F Rear = 4
E Delete
C Delete
front A

Queue
Queues

rear
G Front = 3
F Rear = 4
front E When we try to add
C
A

Queue
Queues

rear
G Front = 3
F Rear = 4
front E When we try to add, it
C will give impression that
queue is full.
A

Queue
Queues

rear
G Front = 3
F Rear = 4
front E When we try to add, it
C will give impression that
queue is full.
A

Queue
Circular Queues

0 1

rear
4 G
2

F
front
Queue 3
Circular Queues

0 1 Now we can add


element.
rear
Because it is
4 G circular.
2
ADD M.
F
front
Queue 3
Circular Queues

rear 0 1 Now we can add


M element.
Because it is
4 G circular.
2
ADD M.
F
front
Queue 3
Circular Queues

rear 0 1 Now we calculate rear


M position by

4 G Rear =
2
(rear+1)%maxSize
F
front
Queue 3
Circular Queues

rear 0 1 ADD K
M

4 G
2

F
front
Queue 3
Circular Queues
rear

0 1 Rear =(0+1)%5 =1
M K

4 G
2

F
front
Queue 3
Implementation details

• A list to store element.


• int* list
• A integer variable to store the maximum Size of array
• int maxQueueSize;
• A integer to show the front of queue
• int front
• A integer to show the rear of queue
• int rear
• A integer to show number of elements in queue.
• int count
Dequeue
Enqueue
Front and rear element
isEmptyQueue and isFullQueue
Priority Queue

 Elements are removed from the Queue on the basis of the


priority.

 Element which has highest priority will be removed first from


queue.
Job Scheduling

 Job will be stored in Queue.


 Highest priority Queue will be removed first.
Job Scheduling in priority Queue.

Enqueue(Jobnumber,Priority)
Enqueue(5,2)

Rear Job number =4


Priority = 0

front Job number =1


Priority =3
Job Scheduling in priority Queue.

Enqueue(Jobnumber,Priority)
Enqueue(5,2)
Add the element.

Rear Job number =4


Priority = 0

front Job number =1


Priority =3
Job Scheduling in priority Queue.

Enqueue(Jobnumber,Priority)
Enqueue(5,2)
Sort the array.
Job number =5
Rear Priority = 2

Job number =4
Priority = 0
front
Job number =1
Priority =3
Job Scheduling in priority Queue.

Job number =4
Rear Priority = 0

Job number =5
Priority = 2

front Job number =1


Priority =3
Job Scheduling in priority Queue.

Dequeue()

Job number =4
Rear Priority = 0

Job number =5
Priority = 2

front Job number =1


Priority =3
Job Scheduling in priority Queue.

Dequeue()

Job number =4
Rear Priority = 0

Job number =5
Priority = 2
front
Job number =1
Priority =3
Job Scheduling in priority Queue.

Dequeue()

Job number =4
Rear Priority = 0

Job number =5
Priority = 2
front
Job number =1
Priority =3
Job Scheduling in priority Queue.

What happened if jobs with


same priority????...

Job number =4
Rear Priority = 0

Job number =5
Priority = 2
front
Job number =1
Priority =3
Job Scheduling in priority Queue.

What happened if jobs with


same priority????...

Rear
Job number =4 Maintain order of jobs.
Priority = 0

Job number =5
Priority = 2
front
Job number =1
Priority =3
Priority Queue
Types of Queues

• linear data structure that:


• Every item has a priority associated with it.
Prior • An element with high priority is dequeued before
an element with low priority.
ity • If two elements have the same priority, they are
Que served according to their order in the queue

ue
Types of Priority Queue

Min Deletes
Priority minimum
Queue element 1st

Deletes Max
maximum Priority
element 1st Queue
Components of a Priority Queue

 Queue
 Item Counter
 If Full and Is Empty checks
 Enqueue Function
 Dequeue Function
 Constructor
Example

 Items to be inserted into Priority Queue : 12,


23, 2, 31, 0
 Size of Queue = 5
 Setting item counter to 0
Enqueue Process

Front = -1
Item Counter = 0 0 1 2 3 4

Item = 12
Front
Item Counter = 1 0 1 2 3 4
12

Front
Item Counter = 1 0 1 2 3 4
23 > 12
Item = 23 12
Front
Item Counter = 2 0 1 2 3 4
23 12
Cont…

Front
Item Counter = 2 0 1 2 3 4 2 < 23
23 12 2 < 12
Item = 2
Front
Item Counter = 3 0 1 2 3 4
23 12 2

Front
0 1 2 3 4 31 > 23
Item Counter = 3
31 > 12
Item = 31 23 12 2
31 > 2
Front
Item Counter = 4 0 1 2 3 4
31 23 12 2
Cont…

Front
Item Counter = 4 0 1 2 3 4
31 23 12 2
Item = 0

Front 0 < 31
0 1 2 3 4
Item Counter = 5 0 < 23
31 23 12 2 0
0 < 12
Queue = Full 0<2
Dequeue Process

Front
0 1 2 3 4
Item Counter = 4 31 23 12 2 0 Dequeued Item = 0

Front
Item Counter = 3 0 1 2 3 4
Dequeued Item = 2
31 23 12 2

Front
Item Counter = 2 0 1 2 3 4
Dequeued Item = 12
31 23 12

Front
Item Counter = 1 0 1 2 3 4
Dequeued Item = 23
31 23
Dequeue Process

Front
0 1 2 3 4
Item Counter = 0 31 Dequeued Item = 0

Front = -1
0 1 2 3 4

Queue = Empty
Implementation – Enqueue ()
void enqueue(int data){
int i = 0;
if(!isFull())
{if(itemCount == 0) // if queue is empty, insert the data
intArray[itemCount++] = data;
else // start from the right end of the queue
{for(i = itemCount - 1; i >= 0; i-- ) // if data is larger, shift existing item to right
end
{if(data > intArray[i])
intArray[i+1] = intArray[i];
else
break;
}
intArray[i+1] = data; // insert the data
itemCount++;}}}
Implementation – Dequeue ()

int removeData()
{
return intArray[--itemCount];
}
Application of Priority Queues

 When identifying shortest paths in graphs


 CPU scheduling:
 CPU can only run one process at a time, but there may be many
jobs of various priorities waiting for their turn.
Queue and Stack

 Making queue using stack.


 Making Stack using Queue.

You might also like