0% found this document useful (0 votes)
11 views37 pages

Unit-2 Queue, Circular Queue

Unit 3 covers the concept of queues as a linear data structure that operates on a FIFO basis, detailing various types such as circular queues, priority queues, and double-ended queues. It outlines operations like enqueue and dequeue, their implementations using arrays and linked lists, and real-life applications of queues in computing. Additionally, it discusses limitations of simple queues and introduces circular queues as a solution to optimize memory usage.

Uploaded by

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

Unit-2 Queue, Circular Queue

Unit 3 covers the concept of queues as a linear data structure that operates on a FIFO basis, detailing various types such as circular queues, priority queues, and double-ended queues. It outlines operations like enqueue and dequeue, their implementations using arrays and linked lists, and real-life applications of queues in computing. Additionally, it discusses limitations of simple queues and introduces circular queues as a solution to optimize memory usage.

Uploaded by

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

Unit - 3

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

Sequential Linked Hierarchical Network


Organization Organization Relationship Relationship

Examples: Array Linked List Trees Graphs


Stack
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
• 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

• Queue can be implemented using either array or linked list.


• When queue is represented using array then it is called static
representation because memory once allocated can not be changed.
• When queue is represented using linked list then it is called dynamic
representation because memory can be changed at run time.
Real life examples:
• People waiting for a bus. The first person standing in the line
will be the first one to get into the bus.
• People standing outside the ticket window of a cinema hall.
The first person in line get the ticket first and thus will be the
first one to move out of it.
• Car Lined for filling petrol, Car lined at toll bridge.
Operations on Queue:
• Insertion operation is called Enqueue.
• Procedure used for insertion is QINSERT

• Deletion operation is called Dequeue.


• Procedure used for deletion is QDELETE
Procedure: QINSERT(Q, F, R, N,
Y)
• This procedure inserts Y at rear end of 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.
• Initially F = 0 & R = 0.
1. [Check for Queue Overflow]
If R >= N
Then write (‘Queue Overflow’)
Return
2. [Increment REAR pointer]
R  R + 1
3. [Insert element]
Q[R]  Y
4. [Is front pointer properly set?]
IF F = 0
Then F  1
Return
Procedure: QINSERT(Q, F, R, N, Y)
1. [Check for Queue Overflow] N=5, R=0, F=0
If R >= N
Then write (‘Queue
Overflow’) QINSERT (Q, F=0, R=0, N=5, Y=50)
Return QINSERT(Q, F=1, R=1, N=5, Y=20)
2. [Increment REAR pointer] QINSERT(Q, F=1, R=2, N=5, Y=80)
R  R + 1 QINSERT(Q, F=1, R=3, N=5, Y=70)
3. [Insert element] QINSERT(Q, F=1, R=4, N=5, Y=45)
Q[R]  Y QINSERT(Q, F=1, R=5, N=5, Y=36)
4. [Is front pointer properly Queue Overflow
set?]
IF F=0
Then F  1
Return
1 2 3 4 5
50 20 80 70 45

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. [Check for Queue 3. [Is Queue Empty?]


Underflow] If F = R
If F = 0 Then F  R  0
Then write (‘Queue Else F  F + 1
Underflow’) 4. [Return Element]
Return(0) Return (Y)
2. [Delete element]
Procedure: QDELETE(Q, F, R)
1. [Check for Queue N=5, R=5, F=1
Underflow]
If F = 0 QDELETE (Q, F=1, R=5)
Then write (‘Queue
QDELETE (Q, F=2, R=5)
Underflow’)
Return(0) QDELETE (Q, F=3, R=5)
2. QDELETE (Q, F=4, R=5)
3. [Delete element]
[Is Queue Empty?] QDELETE (Q, F=5, R=5)
Y
If Q[F]
F = R QDELETE (Q, F=0, R=0)
Then F  R  0
Else F  F + 1 Queue Underflow
4. [Return Element]
Return (Y)

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

Insert ‘A’ R=3 R=4


R=1 Delete ‘A’ Insert ‘E’
F=2 F=3
F=1 A A B C C D

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

This is a major drawback of linear Queue.


(R=4) >= (N=4) (Size of Queue)
Queue Overflow
Queue Overflow, but space is there
with Queue, this leads to the
memory wastage
Possible solutions:
1) Shift the element to the left so that the vacant space can be occupied & utilized
efficiently. But this can be very time-consuming, especially when the queue is
quite large.

2) Use circular queue. In the circular queue, the first index comes right after the last
index.

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


Circular Queue
• A circular queue is a queue where there is no end of 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]


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
00 A B C C D

F R F R F R

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

F R F R F R

Insert ‘B’ R=3 Delete ‘B’


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

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.

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


If R = N Q[R]  Y
Then R  1 4. [Is front pointer properly
Else R  R + 1 set?]
2. [Overflow] IF F = 0
If F = R Then F  1
Then Write(‘Overflow’)
Return Return
Procedure: CQDELETE (F, R,
• Q, N) deletes & returns an element from front end of the
This function
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. [Underflow?] 4. [Increment Front
If F = 0 Pointer]
Then Write(‘Underflow’) IF F = N
Return(0) Then F  1
2. [Delete Element] Else F  F + 1
Y  Q[F] Return(Y)
3. [Queue Empty?]
If F = R
Then F  R  0
Return(Y)
Exampl Perform following operations on Circular queue with size 4 & draw queue after each operation
Insert ‘A’ | Insert ‘B’ | Insert ‘C’ | Insert ‘D’ | Delete one element | Insert ‘E’ | Delete ‘B’ | Insert ‘F’ | Delete four element
e:
CQINSERT (F, R, Q, N, Y) CQDELETE (F, R, Q, N) N=4, R=0, F=0
1. [Reset Rear 1. [Underflow?]
Pointer] If F = 0 CQINSERT (Q, F=0, R=0, N=4, Y=‘A’)
If R = N Then CQINSERT (Q, F=1, R=1, N=4, Y=‘B’)
Then R  1 Write(‘Underflow’) CQINSERT (Q, F=1, R=2, N=4, Y=‘C’)
Else R  R + 1 Return(0) CQINSERT (Q, F=1, R=3, N=4, Y=‘D’)
2. [Overflow] 2. [Delete Element] CQDELETE (F=1, R=4, Q, N=4)
If F = R Y  Q[F]
Then 3. [Queue Empty?]
Write(‘Overflow’) If F = R 1 2 3 4
Return Then F  R  0
3. [Insert Return(Y) A B C D
element] 4. [Increment Front
Q[R]  Y Pointer]
4. [Is front IF F = N F R F R F R R R
pointer properly Then F  1
set?] Else F  F + 1
IF F = 0 Return(Y)
Then F  1
Return
Exampl Perform following operations on Circular queue with size 4 & draw queue after each operation
Insert ‘A’ | Insert ‘B’ | Insert ‘C’ | Insert ‘D’ | Delete one element | Insert ‘E’ | Delete ‘B’ | Insert ‘F’ | Delete four element
e:
CQINSERT (F, R, Q, N, Y) CQDELETE (F, R, Q, N) N=4, R=0, F=0
1. [Reset Rear 1. [Underflow?]
Pointer] If F = 0 CQINSERT (Q, F=2, R=4, N=4, Y=‘E’)
If R = N Then CQDELETE (F=2, R=1, Q, N=4)
Then R  1 Write(‘Underflow’)
Else R  R + 1 Return(0)
2. [Overflow] 2. [Delete Element]
If F = R Y  Q[F]
Then 3. [Queue Empty?]
Write(‘Overflow’) If F = R 1 2 3 4
Return Then F  R  0
3. [Insert Return(Y) E B C D
element] 4. [Increment Front
Q[R]  Y Pointer]
4. [Is front IF F = N F R
pointer properly Then F  1
set?] Else F  F + 1
IF F = 0 Return(Y)
Then F  1
Return
Deque
• A Deque (double ended queue ) is a linear list in which insertion and
deletion are performed from the either end of the structure (from
both ends).
• It is also known as head-tail linked-list, because element can be
deleted from front & back end.
• No elements can be added & deleted from the middle. In computer
memory deque can be implemented as circular array or circular doubly
linked list.

Insertion Deletion
Deletion Insertion

Front Rear
Deque
• There are two variations of Deque:

Input restricted deque- allows Output restricted deque- allows


insertion at only one end (deletion deletion from only one end
from both end) (insertion from both end)
Insertion Deletion Insertion
Deletion Deletion Insertion

Front Rear Front Rear

Deletion Insertion Deletion


Deletion Insertion Insertion

Front Rear Front Rear


Examp Initial deque:
le:
Consider deque given below 0 1 2 3 4 5 6 7 8 9
with 10 memory cells which A B C D E
has left = 1 & right = 5. Draw
the deque structure & Left Right
represent position of left & 1) Insert F on the left
right pointer for each of the 0 1 2 3 4 5 6 7 8 9
following operations. [Array
F A B C D E
starting index = 0]

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, _, _, _, _ 4) Insert I on the right


0 1 2 3 4 5 6 7 8 9
1) Insert F on the left B C D E G H
2) Insert G & H on the right I
3) Delete two alphabets
Left Right
from left 5) Insert J on the left
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 J B C D E G H I
from right
Left Right
Examp 6) Delete two alphabets from right
le:
Consider deque given below 0 1 2 3 4 5 6 7 8 9
with 10 memory cells which J B C D E G H I
has left = 1 & right = 5. Draw
the deque structure & Left Right
represent position of left &
right pointer for each of the 0 1 2 3 4 5 6 7 8 9
following operations. [Array
starting index = 0] J B C D E G H

_ , A, B, C, D, E, _, _, _, _ Left Right

1) Insert F on the left


2) Insert G & H on the right
3) Delete two alphabets
from left
4) Insert I on the right
5) Insert J on the left
6) Delete two alphabets
from right
Priority Queue
• A priority queue is an abstract data type in which each element is
assigned a priority. The priority of the element will be used to
determine the order in which these element will processed. The
general rule of processing the elements of priority queue is:
1) An element with higher priority is processed before an element with an
lower priority.
2) Two element with the same priority are processed on a First come First
served (FCFS) basis.
• 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.
• Priority queue is a modified queue in which an element with highest
priority is retrieved first.
Continue..
• Priority of element can be set based on various factors. Priority queue
are widely used in operating system to execute the highest priority
process first.
• The priority of the process may be set based on the CPU time it
requires to get execute completely.
• Suppose there are 3 processes,
P1, P2 and P3 have CPU time 5, 4 and 7. Here process P2(because of less CPU
time) has highest priority & thus it will be the first one to be executed.
• CPU time is just one factor among several factor.
Implementation of a priority Queue
• There are 2 ways to implement a priority queue.
• Using sorted list
• Using unsorted list
1) Using Sorted List
In this list, when an element has to be taken out, the first element with a highest
priority will be taken out.
2) Unsorted List
• Insertion are always done at end of the list.
• Every time when an element has to be taken out of the list, the element with highest
priority should be searched first then it can be removed.
Note: In unsorted list: It takes it will takes O(1) times to insert an element.
O(n) times to delete an element.
In sorted list: It takes it will takes O(n) times to insert an element.
O(1) times to delete an element.
• Both these techniques are inefficient so usually blend of these two approaches is
adopted that taken roughly O(log n) time or less.
Continue…

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


Continue..
• Above figure shows that how single priority queue can be visualized as
three separate queues each exhibiting a strictly FIFO behaviour.
Element in second queue removed only when the first queue is
empty. Element in third queue removed only when the first and
second queue is empty.
• When element are inserted they are always added at the end of one
of the queue as determined by priority.
• If a single sequential storage structure is used for the priority queue,
according to priority if I want to insert an element in the middle of
the structure. This require the movement of several items. It is better
to split the priority queue into several queue. Each having its own
storage structure.
Array representation of Priority
• Queue
When array is used to implement a priority queue than a
separate queue for each priority number is maintained. Each
of these queue will be implemented using circular queue or
circular arrays.
• Every individual queue has its own FRONT and REAR.
• Use two dimension array for this purpose where each queue
will be allocated the same amount of space.
• FRONT(K) & REAR(K) contain the front & rear value of row K,
Where K= Priority.
Continue..
1 2 3 4 5
Priority FRONT REAR Priority=1
1 3 3 A
2 1 3 Queue Representation
F R
3 4 5
4 4 1
1 2 3 4 5
Priority=2
B C D
1 2 3 4 5
1 A F R

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

You might also like