A circular queue is a queue in which all elements are connected to form a circular. A circular queue can be implemented with an array or a linked list. In the array implementation, the index of the rear element wraps around to the beginning of the array. In the linked list implementation, the last node links back to the front node. In this post, you are going to implement circular queue using an array.

Table of Content
Enqueue
First you declare an array. maxSize is the max length of the array. frontIndex and rearIndex are the indices of the front and rear element respectively. When the queue is empty, they are -1. length is to keep track the numbers of elements in the queue.
Enqueue is to add an element at the back and increase rearIndex by 1. If rearIndex exceeds maxSize, you use modulo operation (“mod” or “%”) to get a new rearIndex within maxSize.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class CircularQueueArray<T> { private T[] queueArray; private int maxSize; // Size of Circular Queue private int frontIndex, rearIndex; private int length = 0; //constructor, Time O(1), Space O(1) @SuppressWarnings("unchecked") public CircularQueueArray(int size) { maxSize = size; frontIndex = -1; rearIndex = -1; queueArray = (T[])new Object[maxSize]; } //Add at the rear, Time O(1), Space O(1) public void enqueue(T value) { if (isFull()) { System.out.println("Queue is full, cannot enqueue "+ value); return; } if (frontIndex == -1) //empty frontIndex = 0; rearIndex = (rearIndex + 1) % maxSize; queueArray[rearIndex] = value; length++; } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class CircularQueueArray { //constructor, Time O(1), Space O(1) constructor(size) { this.maxSize = size; this.frontIndex = -1; this.rearIndex = -1; this.queueArray = []; this.length = 0; } //Add at the rear, Time O(1), Space O(1) enqueue(value) { if (this.isFull()) { console.log("Queue is full, cannot enqueue "+ value); return; } if (this.frontIndex == -1) this.frontIndex = 0; this.rearIndex = (this.rearIndex + 1) % this.maxSize; this.queueArray[this.rearIndex] = value; this.length++; } |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class CircularQueueArray: #constructor, Time O(1), Space O(1) def __init__(self, size): self.maxSize = size self.queueArray = [None] * size self.frontIndex = self.rearIndex = -1 self.length = 0 #Add at the rear, Time O(1), Space O(1) def enqueue(self, value): if self.isFull(): #full print("The circular queue is full, cannot enqueue " + str(value)) return if (self.frontIndex == -1): #empty self.frontIndex = 0 self.rearIndex = (self.rearIndex + 1) % self.maxSize self.queueArray[self.rearIndex] = value self.length += 1 # Check full, Time O(1), Space O(1) def isFull(self): return (self.rear_index + 1) % self.max_size == self.front_index # Check empty, Time O(1), Space O(1) def isEmpty(self): return self.front_index == -1 |
Dequeue in circular queue implementation
The dequeue operation is to remove the front element from a queue. First you check whether the queue is empty. If it is empty, the method should return. You also check whether there is only one element in the queue. If it is, you should reset frontIndex and rearIndex to -1 after removing the last element.
To dequeue, you increases frontIndex by 1. If frontIndex exceeds maxSize, you set frontIndex to index 0. This can be done by using modulo operation (“mod” or “%”). Please note: to dequeue element, you only update frontIndex. The element in the original frontIndex stays in the spot until it is replaced by a new value.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Remove from the front, Time O(1), Space O(1) public T dequeue() { if (isEmpty()) { System.out.println("Queue is empty"); return null; } T item = queueArray[frontIndex]; if (frontIndex == rearIndex) { //last one, reset frontIndex = -1; rearIndex = -1; } else { frontIndex = (frontIndex + 1) % maxSize; } length--; return (item); } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Remove from the front, Time O(1), Space O(1) dequeue() { if (this.isEmpty()) { console.log("Queue is empty"); return null; } var item = this.queueArray[this.frontIndex]; if (this.frontIndex == this.rearIndex) { //one element left this.frontIndex = -1; this.rearIndex = -1; } else { this.frontIndex = (this.frontIndex + 1) % this.maxSize; } this.length--; return (item); } |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Remove from the front, Time O(1), Space O(1) def dequeue(self): if self.isEmpty(): print("The circular queue is empty, cannot dequeue") return item = self.queueArray[self.frontIndex] if (self.frontIndex == self.rearIndex): #one item left self.frontIndex = -1 self.rearIndex = -1 return item else: self.frontIndex = (self.frontIndex + 1) % self.maxSize self.length -= 1 return item |
Peek
Peek is to return the value at firstIndex. You should check whether the queue is empty. If it is not empty, return the value.
Java
1 2 3 4 5 6 7 8 |
//Return front value, Time O(1), Space O(1) public T peek() { if (isEmpty()) { System.out.println("Queue is empty"); return null; } return queueArray[frontIndex]; } |
Javascript
1 2 3 4 5 6 7 8 |
//Return front value, Time O(1), Space O(1) peek() { if (this.isEmpty()) { console.log("Queue is empty"); return null; } return this.queueArray[this.frontIndex]; } |
Python
1 2 3 4 5 6 |
#Return front value, Time O(1), Space O(1) def peek(self): if self.isEmpty(): print("The circular queue is empty, cannot dequeue") return return self.queueArray[self.frontIndex] |
Print
Print is to print all elements in a circular queue. Starting from frontIndex, you use a for-loop to iterate through each slots till to rearIndex. In each iteration, the index i is increased by 1, then mod by maxSize.
Java
1 2 3 4 5 6 7 8 9 10 11 12 |
//Print all, Time O(n), Space O(1), n is number of items in queue public void print() { if (isEmpty()) { System.out.println("Queue is empty"); return; } System.out.print("Circular queue: "); int i; for (i = frontIndex; i != rearIndex; i = (i + 1) % maxSize) System.out.print(queueArray[i] + " "); System.out.println(queueArray[i]); } |
Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Print all, Time O(n), Space O(1), n is number of items in queue print() { if (this.isEmpty()) { console.log("Queue is empty"); return; } var str = "Queue: "; var i; for (i = this.frontIndex; i !=this.rearIndex; i = (i + 1) % this.maxSize) str += this.queueArray[i] + " "; str += this.queueArray[i]; console.log(str); } |
Python
1 2 3 4 5 6 7 8 9 10 11 |
#Print all, Time O(n), Space O(1), n is number of items in queue def print(self): if self.isEmpty(): print("The circular queue is empty") return print("Circular queue: ", end="") i = self.frontIndex while i != self.rearIndex: print(str(self.queueArray[i]) , end=" ") i = (i + 1) % self.maxSize print(self.queueArray[i]) |
Free download
Download CircularQueueArray.java
Download CircularQueueArray.js
Download CircularQueueArray.py