Professional Documents
Culture Documents
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
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.
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.
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.
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
There are few other operations that are used for queue implementation:
3. Peek: Get the value of the front element of the queue without removing it.
Enqueue Operation:
check if the queue is full
Dequeue Operation:
check if the queue is empty
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).
These two conditions can be merged into single using modulo operation:
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
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
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
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.
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
https://levelup.gitconnected.com/visualize-design-and-analyse-the-circular-queue-data-structure-6a4ff6d4359c 9/9