Unit-2 Queue, Circular Queue
Unit-2 Queue, Circular Queue
Queue
Linear Data Structure
Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 3
Queue:
Representation Of Queue
Operations On Queue
Circular Queue
Priority Queue
Array representation of Priority Queue
Double Ended Queue
Applications of Queue
Linked List:
Singly Linked List
Doubly Linked list
Circular linked list
Linked implementation of Stack
Linked implementation of Queue
Applications of linked list
Classification of Data Structure
Data
Structure
Non-Linear
Linear Data
Data
Structure
Structure
10 8 5 80 50 100
Deletion Insertion
Front Rear
• It is ordered collection of homogeneous element and it is non-
primitive linear data structure.
• Total element in queue can be found using Queue= Rear-Front+1.
Index
1 2 3 4 5 6
10 20 40 60 75 12
Front Rear
F R
F R R R R R
Procedure: QDELETE(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.
• R is pointer to the rear element of a queue.
1 2 3 4 5
50 20 80 70 45
F R F F F F F R
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
00 A B C C D
F R
F R F R R
F R F F R F R
Insert ‘B’ R=3 Delete ‘B’ (R=4) >= (N=4) (Size of Queue)
R=2 F=3 Queue Overflow
F=1 A B B C
Queue Overflow, but space is
there with Queue, this leads to
F R F F R
the memory wastage
Applications of Queue:
• Round Robin Scheduling.
• Queue of processes in OS.
• Queue is also used by Operating systems for Job Scheduling.
• Keyboard buffer.
• Queue of request at a web-server.
• 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.
Limitation of Simple(ordinary)
Queue:
Here in this example, even though there are two
Insert ‘E’
R=4 available spaces, it returns overflow condition due to
F=3 C D its properties for insertion and deletion. (where
overflow condition R >= N becomes true)
F R
2) Use circular queue. In the circular queue, the first index comes right after the last
index.
F R F R F R
F R F R F R
FR F R
Procedure: CQINSERT (F, R,
Q,procedure
• This N, Y) inserts Y at rear 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.
Insertion Deletion
Deletion Insertion
Front Rear
Deque
• There are two variations of Deque:
Left Right
_ , A, B, C, D, E, _, _, _, _
2) Insert G & H on the right
1) Insert F on the left 0 1 2 3 4 5 6 7 8 9
2) Insert G & H on the right F A B C D E G
3) Delete two alphabets
from left Left Right
4) Insert I on the right
0 1 2 3 4 5 6 7 8 9
5) Insert J on the left
6) Delete two alphabets F A B C D E G H
from right
Left Right
Examp 3) Delete two alphabets from left
le:
Consider deque given below 0 1 2 3 4 5 6 7 8 9
with 10 memory cells which F A B C D E G H
has left = 1 & right = 5. Draw
the deque structure & Left Right
represent position of left & 0 1 2 3 4 5 6 7 8 9
right pointer for each of the
A B C D E G H
following operations. [Array
starting index = 0]
Left Right
_ , A, B, C, D, E, _, _, _, _ Left Right
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
2 B C D
Array Representation
3 E F
4 I G H
Insertion
• If I want to insert R with priority =3,
1 2 3 4 5 Priority FRONT REAR
1 A 1 3 3
2 B C D 2 1 3
3 R E F 3 4 1
4 I G H 4 4 1
• To insert a new element with priority K in the priority queue, add the element at the
rear end of row K. Where K= row number as well as priority number of the element.
Deletion
• To delete an element, we have to find non empty queue & than
process the front element of the first non empty queue.
• Here first non empty queue is 1 with priority number 1. So first element
is A will be deleted, that means find the element with the smallest K, such
that FRONT(K)!=NULL.
Applications of Queue:
• Round Robin Scheduling.
• Queue of processes in OS.
• Queue is also used by Operating systems for Job Scheduling.
• Keyboard buffer.
• Queue of request at a web-server.
• 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.
Thank you