Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views44 pages

Week4 Queue

The document discusses queues, their operations, implementation as arrays and circular queues. It also covers priority queues and how they can be used for job scheduling by removing highest priority jobs first. The concepts of job class, and implementing priority queues and their operations like enqueue and dequeue are also explained.

Uploaded by

Anees khan
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)
20 views44 pages

Week4 Queue

The document discusses queues, their operations, implementation as arrays and circular queues. It also covers priority queues and how they can be used for job scheduling by removing highest priority jobs first. The concepts of job class, and implementing priority queues and their operations like enqueue and dequeue are also explained.

Uploaded by

Anees khan
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/ 44

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
Constructor
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
Job Class
Priority Queue (Implemention)
EnQueue (Implemention)
DeQueue (Implementation)
Queue and Stack

 Making queue using stack.


 Making Stack using Queue.

You might also like