0% found this document useful (0 votes)
13 views

Unit2 2 Queue

The document discusses queues, which are linear data structures that follow the first-in, first-out (FIFO) principle. Elements are inserted at the rear of the queue and deleted from the front. Circular queues improve on standard queues by connecting the front and rear pointers to save memory. Common applications of queues include job scheduling, network packet handling, and breadth-first search algorithms.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit2 2 Queue

The document discusses queues, which are linear data structures that follow the first-in, first-out (FIFO) principle. Elements are inserted at the rear of the queue and deleted from the front. Circular queues improve on standard queues by connecting the front and rear pointers to save memory. Common applications of queues include job scheduling, network packet handling, and breadth-first search algorithms.

Uploaded by

Ritika Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit-2

Linear Data
Structure Queue
Queue
 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

2
Applications of Queue
 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.
3
Procedure: Enqueue (Q, F, R, N,Y)
 This procedure inserts Y at rear end of 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 is pointer to the rear element of a queue. F

1. [Check for Queue Overflow] N=3, R=0, F=0


If R >= N
Then write (‘Queue Overflow’) F =10
Return R =3201
2. [Increment REAR pointer] Enqueue (Q, F, R,
R  R + 1
N=3,Y=5)
Enqueue (Q, F, R,
3. [Insert element]
N=3,Y=20)
Enqueue (Q, F, R,
Q[R]  Y
4. [Is front pointer properly set?] N=3,Y=80)
Enqueue (Q, F, R,
IF F=0 N=3,Y=3)
Queue Overflow
Then F  1
Return
4
Function: Dequeue (Q, F, R)
 This function deletes & returns an element from front end of the Queue.
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue. Case No 1:
F=0, R=0
 R is pointer to the rear element of a queue.
1. [Check for Queue Underflow] Queue Underflow
If F = 0
Then write (‘Queue Underflow’) Case No 2: FR
Return(0) F=3, R=3
2. [Delete element]
Y  Q[F] F=0, R=0 50
3. [Is Queue Empty?]
If F = R
Then F  R  0 Case No 3: F R
Else F  F + 1 F=1, R=3
4. [Return Element]
5 -8 50
Return (Y) F=2, R=3
5
Example of Queue Insert / Delete
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’


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

FR F R FR

R=1 Insert ‘A’ R=3 Delete ‘A’ R=4 Insert ‘E’


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

FR F R F R

Insert ‘B’ R=3 Delete ‘B’ (R=4) >= (N=4) (Size of


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

6
Circular Queue
 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], 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]

7
Procedure: CQINSERT (F, R, Q, N, Y)
 This procedure inserts Y at rear end of the Circular Queue. F R

 Queue is represented by a vector Q containing N elements. 8 15


 F is pointer to the front element of a queue.
 R is pointer to the rear element of a queue. F R

1. [Reset Rear Pointer] 3. [Insert element]


8 15
If R = N Q[R]  Y
Then R  1 4. [Is front pointer
Else R  R + 1 properly set?]
2. [Overflow] IF F=0 F R
If F=R Then F  1
Then Write(‘Overflow’) Return 23 6 8 15
Return

8
Function: CQDELETE (F, R, Q, N)
 This function deletes & returns an element from front end of the F R

Circular Queue.
6
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue. R F

 R is pointer to the rear element of a queue.


2 6 4
1. [Underflow?] 4. Increment Front Pointer]
If F = 0 IF F = N
Then Write(‘Underflow’) Then F  1 F R
Return(0) Else F  F + 1
2. [Delete Element] Return(Y)
Y  Q[F] 6 8 4
3. [Queue Empty?]
If F = R
Then F  R  0
Return(Y)
9
Example of CQueue Insert / Delete
Perform following operations on Circular 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’
F=1 F=3
0 0 A B C C D

FR F R FR

R=1 Insert ‘A’ R=3 Delete ‘A’ R=1 Insert ‘E’


F=2 F=3
F=1 A A B C E C D

FR F R F R

Insert ‘B’ R=3 Delete ‘B’


R=2 F=3
F=1 A B B C

FR F R

10
DQueue
 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
 Input restricted dqueue – allows insertion at only one end
 Output restricted dqueue – allows deletion from only one end

Insertion Deletion
Deletion Insertion
 Dqueue Algorithms
Fron Rea
 DQINSERT_REAR is same as QINSERT (Enqueue)
t r
 DQDELETE_FRONT is same as QDELETE (Dequeue)
 DQINSERT_FRONT
 DQDELETE_REAR

11
Procedure: DQINSERT_FRONT (Q,F,R,N,Y)
 This procedure inserts Y at front end of the Circular Queue.
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue.
 R is pointer to the rear element of a queue.
1. [Overflow?]
F R
If F = 0
Then Write(‘Empty’)
Return Overflow
10 89 7
If F = 1
Then Write(‘Overflow’)
Return F R
2. [Decrement front Pointer]
F  F - 1
3. [Insert Element?] 50 10 89 7
Q[F]  Y
Return
12
Function: DQDELETE_REAR(Q,F,R)
 This function deletes & returns an element from rear end of the Queue.
 Queue is represented by a vector Q containing N elements.
 F is pointer to the front element of a queue.
 R is pointer to the rear element of a queue.
1. [Underflow?] FR
If R = 0
Then Write(‘Underflow’)
Return(0) 7
2. [Delete Element]
Y  Q[R]
F R
3. [Queue Empty?]
IF R = F
Then R  F  0
10 89 7
Else R  R – 1
4. [Return Element]
Return(Y)

13
Priority Queue
 A queue in which we are able to insert & remove items from any position based on some
property (such as priority of the task to be processed) is often referred as priority queue.
 Below fig. represent a priority queue of jobs waiting to use a computer.
 Priorities are attached with each Job
 Priority 1 indicates Real Time Job
 Priority 2 indicates Online Job
 Priority 3 indicates Batch Processing Job

 Therefore if a job is initiated with priority i, it is inserted immediately at the end of list of
other jobs with priorities i.
 Here jobs are always removed from the front of queue.

14
Priority Queue Cont…
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 Viewed as a set of queue

15

You might also like