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
Data structures and Java collections

What is wrap around technique used in a circular queue?

In a circular queue, you declare frontIndex and rearIndex to keep track the front and rear positions. The wrap around is to use modulo operation to make sure frontIndex and rearIndex with the range of maxSize of the array. For example, to enqueue, the formula is rearIndex = (rearIndex + 1) % maxSize. To dequeue, it is frontIndex = (frontIndex + 1) % maxSize.