Circular queue is a queue in which all elements are connected to form a circular. Circular queue can be implemented with array and linked list. In array implementation, the front and rear wrap around (modulo operation) to the beginning of the array. In linked list implementation, the last node links back to the front node. In this post, circular queue implementation is using array.

Table of Content


Map of queue implementations

Part 1 – Queue implementation with linked list

you are here

Part 2 – Circular queue implementation with array
Part 3 – Circular linked list (Circular queue)
Part 4 – Doubly linked list (Deque)


Enqueue in circular queue implementation

First we declare an array. In Java, if you use array (not ArrayList), you have to specify the maxSize. If the array reaches the maxSize, you cannot add more element. The frontIndex and rearIndex is the index of the front element and rear element. When the queue is empty, they are -1. The length is to keep track of the numbers of elements in the queue.

To enqueue is to add element at the rear. The rearIndex is increased by 1. If the rearIndex exceeds the maxSize, we use modulo operation (or “mod”, “%”) to get the new rearIndex with the maxSize range.

Java

Javascript

Python

Doodle

circular queue enqueue


Dequeue in circular queue implementation

The dequeue operation is to remove the front node from the queue. First we check whether the queue is empty. If it is empty, the method should return. We also check whether there is only one element in the queue. If it is, we should reset the frontIndex and rearIndex to -1 after removing the last element.

To dequeue, we increases the frontIndex by 1. Like enqueue, if the frontIndex exceeds the maxSize, we have to put the element back to index 0. We use modulo operation (or “mod”, “%”) to get the new frontIndex. Please note to dequeue element, we only update the frontIndex. The original value stays in the spot until it is replaced with new value.

Java

Javascript

Python

Doodle

circular queue dequeue


Peek

Peek is to return the value at the firstIndex. The same as dequeue, we should check whether the queue is empty. If it is not empty, return the value.

Java

Javascript

Python


Print

Print is to print all elements in the circular queue. Starting from the frontIndex, a for loop or while loop is used to iterate through each slots till to the rearIndex. Like enqueue and dequeue, the index i is increased by 1, then mod the maxSize.

Java

Javascript

Python


Free download

Download circular queue implementation in Java, JavaScript and Python
Download aggregate Data Structures implementations in Java
Download aggregate Data Structures implementations in JavaScript
Download aggregate Data Structures implementations in Python

When to implement circular queue using array and when to use linked list?

circular queue

When there is a lot dequeue and enqueue at the same time, it is better to use array. The discard space can be reused. This makes better use of memory space. When you cannot predict how long the circular queue will be, you use circular linked list. It is flexible to expand or shrink.

What is the formula of circular queue using array?

In the array implementation, you declare frontIndex and rearIndex to keep track the position of the front and rear element. They both start at -1. To wrap around so that the frontIndex and rearIndex are within the range of the array length, you use modulo operator %. To enqueue, the formula is rearIndex = (rearIndex + 1) % maxSize. To dequeue, it is frontIndex = (frontIndex + 1) % maxSize.