You are on page 1of 9

14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level

Gupta | Feb, 2021 | Level Up Coding

You have 2 free member-only stories left this month. Sign up for Medium and get an extra one

Visualize, Design, and Analyse the Circular


Queue Data Structure
Know the Circular Queue data structure and operations related to it.

Vikram Gupta
Feb 7 · 4 min read

Circular Queue

Hi friends, I have already covered all sorting and searching algorithms. You can find
them here. I would request you to read and comment on those articles so that I can
improve them over the period. Now, this is another data structure article where I’m
going to cover circular queue data structure.

Why Do We Need Circular Queue?


https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 1/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

In normal queue Implementation, after few enQueue and deQueue operations, there
are empty spaces(positions) left out in the queue that are not being used till both the
front and rear pointers are reset to -1.

Wastage of memory in normal queue implementation.

Hence to utilize the empty spaces for adding new items in the queue we can use a
circular queue implementation in which both pointers front and rear are circular
increments that mean after reaching the end of the queue the pointers come back to
the start of the queue.

A circular Queue is a linear data structure in which the operations are performed
based on the FIFO (First In First Out) principle and it works by the process of circular
increment i.e. when we try to increment the pointer and we reach the end of the queue,
we start from the beginning of the queue.

Basically, the circular increment is a modulo operation.

Eg. REAR =(REAR+1)%SIZE and FRONT=(FRONT+1)%SIZE

Circular Queue Operations:


There are mainly two operations that can be performed on a circular queue:

https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 2/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

1. Enqueue: Adding an item to the end or rear of the queue.

2. Dequeue: Removing an item from the front of the queue.

There are few other operations that are used for queue implementation:

1. IsEmpty: Check if the queue is empty.

2. IsFull: Check if the queue is full.

3. Peek: Get the value of the front element of the queue without removing it.

Let’s Understand the Working of the Circular Queue :


For Circular Queue implementation, two pointers FRONT and REAR are used.

1. The FRONT pointer points to the first element of the queue.

2. The REAR pointer points to the last element of the queue.

Note that initially, FRONT and REAR are set to -1.

Enqueue Operation:
check if the queue is full

for the first element, set the value of FRONT to 0

increase the REAR index by 1

add the new element at the position pointed by REAR

Dequeue Operation:
check if the queue is empty

return the value pointed by FRONT

increase the FRONT index by 1

When there are no elements in the queue, reset the values of FRONT and REAR to
-1. (i.e after removing the single (last ) element in the queue).

Queue empty condition:


https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 3/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

if FRONT == -1 then the queue is empty.

Queue full condition:

1. FRONT == 0 and REAR == (SIZE - 1) :- Queue full

2. FRONT == (REAR + 1) :- Queue full

These two conditions can be merged into single using modulo operation:

FRONT == (REAR+1) % SIZE :- Queue full

Visualizing Circular Queue:

https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 4/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

Circular Queue Logic Flow

Circular Queue Implementation:

1
2 public class CircularQueue {
3
4 int front;
5 int rear;
6 int capacity;
7 int queue[];
8
9 public CircularQueue(int front, int rear, int capacity) {
10 this.front = front;
11 this.rear = rear;
12 this.capacity = capacity;
13 this queue new int[capacity];
https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 5/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding
13 this.queue = new int[capacity];
14 }
15
16 void enQueue(int element) {
17 if (isFull()) {
18 System.out.println("Queue is Full!!!");
19 return;
20 } else {
21 if (front == -1) {
22 front = 0;
23 }
24 rear = (rear + 1) % capacity;
25 queue[rear] = element;
26 System.out.println("Added : " + element);
27 }
28 displayQueue();
29 }
30
31 int deQueue() {
32 if (isEmpty()) {
33 System.out.println("Queue is Empty!!!");
34 return -1;
35 } else {
36 int element = queue[front];
37 System.out.println("Removed : " + element);
38 queue[front] = 0;
39 if (front == rear) {
40 front = -1;
41 rear = -1;
42 } else {
43 front = (front + 1) % capacity;
44 }
45 displayQueue();
46 return element;
47 }
48
49 }
50
51 boolean isEmpty() {
52 return front == -1 ? true : false;
53 }
54
55 boolean isFull() {
56 return (front == (rear + 1) % capacity) ? true : false;
57 }
58
59 void displayQueue() {
60 System.out.println("-------- Queue content ----------");
https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 6/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

61 for (int i = 0; i < queue.length; i++) {


62 System.out.print("\t" + queue[i]);
63 }
64 System.out.println("\n---------------------------------");
65 }
66
67 public static void main(String[] args) {
68
69 CircularQueue circularQueue = new CircularQueue(-1, -1, 3);
70 circularQueue.deQueue();//check queue empty
71 circularQueue.enQueue(10);
72 circularQueue.enQueue(20);
73 circularQueue.enQueue(30);
74 circularQueue.enQueue(40);//check queue full
75 circularQueue.deQueue();
76 circularQueue.deQueue();
77 circularQueue.enQueue(40);
78 circularQueue.enQueue(50);
79 circularQueue.enQueue(60);//check queue full
80 }
81 }

CircularQueue java hosted with ❤ by GitHub view raw


Circular Queue Implementation

Output:

Queue is Empty!!!
Added : 10
-------- Queue content ----------
10 0 0
---------------------------------
Added : 20
-------- Queue content ----------
10 20 0
---------------------------------
Added : 30
-------- Queue content ----------
10 20 30
---------------------------------
Queue is Full!!!
Removed : 10
-------- Queue content ----------
0 20 30
---------------------------------
Removed : 20
-------- Queue content ----------
0 0 30
---------------------------------
Added : 40

https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 7/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

-------- Queue content ----------


40 0 30
---------------------------------
Added : 50
-------- Queue content ----------
40 50 30
---------------------------------
Queue is Full!!!

Time Complexity Analysis:


Both the enQueue and deQueue operations are single statement execution. These
operations take a constant amount of time. Hence time complexity for these operations
will be O(1).

Applications:
Queues are used in job/disk scheduling algorithms in Operating Systems.

The operating system uses queues for (queuing messages, IO requests, mouse
movements, etc).

Thank you for reading this article. I hope you found this article useful. If yes then give a
clap and you may read other data structures and algorithm articles listed below.

Visualizing, Designing, and Analyzing the Merge Sort


Algorithm.
Know complete analysis of Merge Sort Algorithm.
levelup.gitconnected.com

https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 8/9
14/02/2021 Visualize, Design, and Analyse the Circular Queue Data Structure | by Vikram Gupta | Feb, 2021 | Level Up Coding

Visualizing, Designing, and Analyzing the Heap Sort


Algorithm.
Complete analysis of HeapSort Algorithm.
levelup.gitconnected.com

Visualizing, Designing, and Analyzing the Quick Sort


Algorithm.
Know the most asked sorting data structure interview question.
levelup.gitconnected.com

Data Structures Programming Software Development Circular Queue Queue

About Help Legal

Get the Medium app

https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 9/9

You might also like