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

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

Ch-2 Queue New

Uploaded by

bhavyaladumor2
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)
14 views22 pages

Ch-2 Queue New

Uploaded by

bhavyaladumor2
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

 

Prepared By: Dr. Dhruti Sharma, IT, SCET


 A linear list which permits deletion to be performed at one end
of the list and insertion at the other end is called queue.
 The information in such a list is processed FIFO (first in first out)
or FCFS (first come first served) manner.
 Front is the end of queue from that deletion is to be performed.
 Rear is the end of queue at which new element is to be inserted.
 Insertion operation is called Enqueue & deletion operation is
called Dequeue. 10 8 5 80 50 100

Deletion Insertion

Front Rear
 Queue of people at any service point such as ticketing etc.
 Queue of air planes waiting for landing instructions.
 Queue of processes in OS.
 Queue is also used by Operating systems for Job Scheduling.
 When a resource is shared among multiple consumers. E.g., in case of
printers the first one to be entered is the first to be processed.
 When data is transferred asynchronously (data not necessarily received
at same rate as sent) between two processes. Examples include IO
Buffers, pipes, file IO, etc.
 Queue is used in BFS (Breadth First Search) algorithm. It helps in
traversing a tree or graph.
 Queue is used in networking to handle congestion.
 This procedure inserts Y at rear end of Queue.
R
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue. N=3, R=0, F=0
 R is pointer to the rear element of a queue. F R
1. [Check for Queue Overflow] QINSERT(Q, F, R, N=3,Y=5)
If R >= N N=3, R=1, F=0 5
Then write (‘Queue Overflow’)
Return R
2. [Increment REAR pointer] F
R  R + 1 QINSERT(Q, F, R, N=3,Y=20) 5 20
3. [Insert element] N=3, R=2, F=0
Q[R]  Y
4. [Is front pointer properly set?] QINSERT(Q, F, R, N=3,Y=80) F R
IF F=0
N=3, R=3, F=0
Then F  1 5 20 80
Return QINSERT(Q, F, R, N=3,Y=3)
Queue Overflow
F
 This function deletes & returns an element from front end of the Queue. R
 Queue is represented by a vector Q containing N elements.
5 20 80
 F is pointer to the front element of a queue. R=3, F=1
 R is pointer to the rear element of a queue. R
F
1. [Check for Queue Underflow]
QDELETE(Q, F, R)
If F = 0 20 80
Then write (‘Queue Underflow’) R=3, F=2, Y=5
Return(0)
F R
2. [Delete element] QDELETE(Q, F, R)
Y  Q[F] 80
3. [Is Queue Empty?]
R=3, F=3, Y=20
If F = R F
Then F  R  0 QDELETE(Q, F, R) R
Else F  F + 1 R=0, F=0, Y=80
4. [Return Element]
Return (Y) QDELETE(Q, F, R)
F
Queue Underflow
Perform following operations on queue with size 4 & draw queue after each operation
Insert ‘A’ | Insert ‘B’ | Insert ‘C’ | Delete ‘A’ | Delete ‘B’ | Insert ‘D’ | Insert ‘E’

Empty Queue R=3 Insert ‘C’ R=4 Insert ‘D’


00 F=1 A B C F=3 C D

FR F R F R

R=1 Insert ‘A’ R=3 Delete ‘A’ R=4 Insert ‘E’ R=N
F=1 A F=2 B C F=3 C D Queue Overflow

FR F R F R
Insert ‘B’ R=3 Delete ‘B’
R=2 F=3 Queue Overflow, but space is there with
A B C Queue, this leads to the memory wastage
F=1
F R FR

Prepared By: Dr. Dhruti Sharma, IT, SCET


 A more suitable method of representing simple queue which prevents an excessive
use of memory is to arrange the elements Q[1], Q[2]….,Q[n] in a circular fashion
with Q[1] following Q[n] that again following Q[1], this is called circular queue.
 In circular queue the last node is connected back to the first node to make a
circle.
 Circular queue is a linear data structure. It follows FIFO principle.
 It is also called as “Ring buffer”.

Q[1] Q[2] Q[n]


CQINSERT(Q, F, R, N,Y=8)
FR
 This procedure inserts Y at rear end of the Circular Queue.
 Queue is represented by a vector Q containing N elements. 8
 F , R are pointer to the front and rear elements of a queue.
CQINSERT(Q, F, R, N,Y=15)
 Initially, F=R=-1 CQINSERT(Q, F, R, N,Y=20)

1. [Check Overflow] 3. [Insert element] CQINSERT(Q, F, R, N,Y=30)


If F=(R+1)%N Q[R]  Y F R
Then Write(‘Overflow’) 4. Return
Return
8 15 20 30
2. [Reset F and R]
If F=-1 && R=-1
CQINSERT(Q, F, R, N,Y=40)
Then F=R=0
Else If R=N-1 && F!=0 F R
Then R=0
Else 8 15 20 30
R=(R+1)%N
Overflow
 This function deletes & returns an element from front end of the CircularF Queue. R
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue. 8 15 20 30
 R is pointer to the rear element of a queue. CQDELETE(Q, F, R, N) F R
1. [Underflow?] Else If F=N-1
If F=-1 Then F=0 15 20 30
Then Write(‘Underflow’) Else
CQDELETE(Q, F, R, N)
Return(0) F=F+1
CQDELETE(Q, F, R, N)
2. [Delete Element] 4. Return(Y)
CQDELETE(Q, F, R, N)
Y  Q[F]
3. [If only one element?]
If F = R
Then F  R  -1
FR
 A DQueue (double ended queue) is a linear list in which insertion and
deletion are performed from the either end of the structure.
 There are two variations of Dqueue
o Input restricted dqueue – Insertions can be done only at one of the ends, while
deletions can be done from both ends.
o Output restricted dqueue – Deletions can be done only at one of the ends, while
insertions can be done on both ends.
 Dqueue Algorithms
o DQINSERT_REAR is same as QINSERT (Enqueue)
o DQDELETE_FRONT is same as QDELETE (Dequeue)
o DQINSERT_FRONT
o DQDELETE_REAR
 This procedure inserts Y at front end of the Circular Queue.
 Queue is represented by a vector Q containing N elements.
F R
 F is pointer to the front element of a queue.
 R is pointer to the rear element of a queue. 10 89 7
1. [Check for the front position]
If F <= 1 DQINSERT_FRONT(Q,F,R,N,Y=11)
Then Write(‘Can’t add item at front’) Can’t add item at front
Return F R
2. [Decrement front Pointer]
F  F - 1
3. [Insert Element?] 10 89 7
Q[F]  Y
Return DQINSERT_FRONT(Q,F,R,N,Y=11)
F R
50

11 10 89 7
 This function deletes & returns an element from rear end of the Queue.
F R
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue.
7
 R is pointer to the rear element of a queue.
DQDELETE_REAR(Q,F,R)
1. [Underflow?]
If R = 0 F=R=0
Then Write(‘Can’t delete value from rear’)
Return(0)
R F
2. [Delete Element]
Y  Q[R] 7
3. [Queue Empty?]
10
IF R = F
Then R  F  0 DQDELETE_REAR(Q,F,R)
Else R  R – 1 Can’t delete item from rear
4. [Return Element]
Return(Y)
 A priority queue is a data structure that stores elements with associated priorities.
 A new element is added at the position identified by its priority.
 Elements with higher priority are typically retrieved or removed before elements
with lower priority.
 Priority queues can be implemented using arrays, heaps, or linked lists.
 Ascending Order Priority Queue :
o In this queue, elements with lower values have higher priority.
o For example, with elements 4, 6, 8, 9, and 10, 4 will be dequeued first since it has the smallest
value, and the dequeue operation will return 4.
 Descending order Priority Queue :
o Elements with higher values have higher priority.
 Insert (value, priority):
o If the newly inserted item is of the highest priority, then it is inserted at the top.
o Otherwise, it is inserted in such a way that it is accessible after all higher priority items are
accessed.
 Delete():
o It removes the highest priority item which is typically available at the top.
o Once item is removed, the next priority item will be automatically moved at the top.
 Peek ():
o It returns the highest priority item (which is typically available at the top) and does not make
any change to the priority queue.
 In a normal queue, elements are processed in the order they arrive, following the First-In,
First-Out (FIFO) principle. This means that the first element added to the queue will be the
first one to be removed.
 In contrast, a priority queue processes elements based on their priority, not just their order
of arrival.
 Each element in a priority queue is associated with a priority value. Elements with higher
priority are processed before those with lower priority. If two elements have the same
priority, they are processed based on their arrival order.
 The main difference lies in the processing order: normal queues strictly follow the FIFO
rule, while priority queues follow a priority-based rule, allowing for more flexible and
efficient handling of tasks that require prioritized processing.

Prepared By: Dr. Dhruti Sharma, IT, SCET


Task R1 R2 … Ri-1 O1 O2 … Oj-1 B1 B2 … Bk-1 …
Priority 1 1 … 1 2 2 … 2 3 3 … 3 …

Ri Oj Bk
Priority Queue viewed as a single queue with insertion allowed at any position

Priority - 1 R1 R2 … Ri-1 Ri

Priority - 2 O1 O2 … Oj-1 Oj

Priority - 3 B1 B2 … Bk-1 Bk

Priority Queue viewed as a set of queue


 In this approach, a new element is inserted at the appropriate position
and all other entries are shifted by one position to the right, thus
keeping the entries in the array in order.
 Thus, the largest item is always at the end, and the code for remove
the maximum in the priority queue is the same as for pop in the stack.

Prepared By: Dr. Dhruti Sharma, IT, SCET


Prepared By: Dr. Dhruti Sharma, IT, SCET
 Task Scheduling:
o Queues can be used to schedule tasks based on priority or the order in which they were
received.
 Resource Allocation:
o Queues can be used to manage and allocate resources, such as printers or CPU processing
time.
 Message Buffering:
o Queues can be used to buffer messages in communication systems, such as message queues
in messaging systems or buffers in computer networks.
 Event Handling:
o Queues can be used to handle events in event-driven systems, such as GUI applications or
simulation systems.

Prepared By: Dr. Dhruti Sharma, IT, SCET


 Traffic Management:
o Queues can be used to manage traffic flow in transportation systems, such as airport control
systems or road networks.
 Operating systems:
o Operating systems often use queues to manage processes and resources.
 Network protocols:
o Network protocols like TCP and UDP use queues to manage packets that are transmitted over
the network.
 Printer queues :
o In printing systems, queues are used to manage the order in which print jobs are processed.
 Web servers:
o Web servers use queues to manage incoming requests from clients. Requests are added to the
queue as they are received, and they are processed by the server in the order they were
received.
Prepared By: Dr. Dhruti Sharma, IT, SCET
 

Prepared By: Dr. Dhruti Sharma, IT, SCET

You might also like