Module 2.2.2 Circular Queue
Module 2.2.2 Circular Queue
Data Structure
CSC303
CIRCULAR QUEUES
Darakhshan Khan
Why Was the Concept of Circular Queue Data Structure
CSC303
Introduced?
• Implementation of a linear queue brings the drawback of memory wastage.
• In the case of a linear queue, when the rear pointer reaches the MaxSize of a queue,
there might be a possibility that after a certain number of dequeue() operations, it
will create an empty space at the start of a queue.
• Additionally, this newly created empty space can never be reutilized as the rear
pointer reaches the end of a queue. Hence, the concept of the circular queue is
introduced to overcome this limitation.
Darakhshan Khan
Why Was the Concept of Circular Queue Data Structure
CSC303
Introduced?
• The rear pointer arrives at the beginning of a queue with the help of a
circular link to reutilize the empty space to insert a new element.
• This simple addition of a circular link resolves the problem of memory
wastage in the case of queue implementation.
Darakhshan Khan
Data Structure
What is Circular Queue? CSC303
• A circular queue
– is an extended version of a linear
queue
– follows the First In First Out principle
with the exception that it connects the
last node of a queue to its first by
forming a circular link.
– Hence, it is also called a Ring Buffer.
Algorithm
Checks if queue is empty
front = -1
rear = -1
5
Darakhshan Khan
Data Structure
Algorithm
Checks if queue is full
front = 0 rear = 2
A B C
front = 1
A B C
rear = 0 6
Darakhshan Khan
Data Structure
Algorithm
front = 0
A B C
rear = 2
8
Darakhshan Khan
Algorithm
front = -1
rear = -1
9
Darakhshan Khan
Algorithm
front = 1
A B C
rear = 0 rear = 2
10
Darakhshan Khan
front = 0
Algorithm
A B C
rear =rear
1 =2
front = 1
B C
rear = rear
1 =2 11
Darakhshan Khan
Data Structure
• The dequeue operation is used to delete element from the front end
of queue.
• However, before deleting the value, we must first check if
FRONT = REAR= – 1 because if that is the case, then it means the
queue is empty and no more deletions can be done.
• If queue has only one element i.e FRONT = REAR, then make
FRONT = REAR = – 1
• If FRONT = MAX – 1 , then make FRONT = 0
• Otherwise , just increment FRONT.
12
Darakhshan Khan
Algorithm
front = -1
rear = -1
13
Darakhshan Khan
Algorithm
front = 2
rear = 2
14
Darakhshan Khan
Algorithm
front = 0 front = 2
A C
rear = 0
15
Darakhshan Khan
Algorithm
front =front
0 =1
A B C
rear = 2
16
Darakhshan Khan
A B C
front = 3
F G D E
rear = 1 17