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

Module 2.2.2 Circular Queue

The document explains the concept of circular queues, which are designed to overcome the limitations of linear queues, particularly memory wastage. It details operations such as isEmpty, isFull, Enqueue, Dequeue, and Display, along with their algorithms. Circular queues maintain the First In First Out principle while connecting the last node back to the first, forming a circular link.

Uploaded by

shrutidgaf
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)
4 views

Module 2.2.2 Circular Queue

The document explains the concept of circular queues, which are designed to overcome the limitations of linear queues, particularly memory wastage. It details operations such as isEmpty, isFull, Enqueue, Dequeue, and Display, along with their algorithms. Circular queues maintain the First In First Out principle while connecting the last node back to the first, forming a circular link.

Uploaded by

shrutidgaf
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/ 17

Darakhshan Khan

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.

• The circular queue resolves the


memory wastage problem with
the help of a circular link.
Darakhshan Khan
Data Structure

1. isEmpty() operation CSC303

Algorithm
Checks if queue is empty

front = -1
rear = -1

5
Darakhshan Khan
Data Structure

2. isFull() operation CSC303

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

3. Enqueue() operation CSC303

• The enqueue operation is used to insert an element into the queue.


The new element is added at the rear end of the queue.
• If FRONT = 0 and REAR = MAX – 1, OR If FRONT = REAR+1
–then the circular queue is full.
• If FRONT = – 1 and REAR = – 1, then the circular queue is empty.
–So , make FRONT = REAR =0 and add element.
• If FRONT != 0 and REAR = MAX – 1, then,
–Make REAR =0 and add element.
• Otherwise, increment rear and add element.
7
Darakhshan Khan

3. Enqueue() operation Data Structure


CSC303

Algorithm
front = 0

A B C

rear = 2

8
Darakhshan Khan

3. Enqueue() operation Data Structure


CSC303

Algorithm

front = -1
rear = -1

9
Darakhshan Khan

3. Enqueue() operation Data Structure


CSC303

Algorithm
front = 1

A B C

rear = 0 rear = 2

10
Darakhshan Khan

3. Enqueue() operation Data Structure


CSC303

front = 0
Algorithm
A B C

rear =rear
1 =2
front = 1

B C

rear = rear
1 =2 11
Darakhshan Khan
Data Structure

4. Dequeue() operation CSC303

• 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

4. Dequeue() operation Data Structure


CSC303

Algorithm

front = -1
rear = -1

13
Darakhshan Khan

4. Dequeue() operation Data Structure


CSC303

Algorithm
front = 2

rear = 2

14
Darakhshan Khan

4. Dequeue() operation Data Structure


CSC303

Algorithm
front = 0 front = 2

A C

rear = 0

15
Darakhshan Khan

4. Dequeue() operation Data Structure


CSC303

Algorithm
front =front
0 =1

A B C

rear = 2

16
Darakhshan Khan

5. Display() operation Data Structure


CSC303

Prints all elements in queue Algorithm


front = 0 rear = 2

A B C

front = 3

F G D E

rear = 1 17

You might also like