How to Implement a Circular Queue in Java ? Last Updated : 13 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A Circular Queue is a queue in which we can insert an element at the start of the Array even if our rare is reached at the last index and if we have space at the start of the array. This reduces the problem of inefficient use of array space. Once the array is filled till the last and if we have space at the beginning of an array, we fill the element at the front we perform insertion in a circular manner that's why it is known as a circular queue. Approach to Implementing a Circular Queue If we observe there is a problem in the above method. While inserting the element in a queue instead of incrementing the index by one what we will do here is we will increase the index as (index+1) % size.Because of this increment when we reach the end of the queue and do increment, we will again reach index 0.If that index is empty which can be because of the removal of an element we can reutilize that space to insert new elements.In this method, when we go to the end index, we again come to the front of the queue which is why it is known as a circular queue.Program to Implement a Circular Queue in JavaBelow is the Program to implement a Circular Queue in Java: Java // Java Program to implement a Circular Queue in Java import java.util.*; public class CircularQueue { // Maximum size of the circular queue private int maxSize; // Array to store the circular queue elements private int[] queueArray; // Index of the front element private int front; // Index of the rear element private int rear; // constructor to initialize the circular queue with a given size public CircularQueue(int size) { maxSize = size; queueArray = new int[maxSize]; // Initially set front and rear to -1 to indicate an empty queue front = -1; rear = -1; } // method to enqueue (add) an item to the circular queue public void enqueue(int item) { if (isEmpty()) { front = 0; rear = 0; queueArray[rear] = item; } else { // Circular increment of rear index rear = (rear + 1) % maxSize; if (rear == front) { System.out.println("Queue is full. Cannot enqueue."); // Reset rear to its previous value rear = (rear - 1 + maxSize) % maxSize; } else { queueArray[rear] = item; } } } // method to dequeue (remove) an item from the circular queue public int dequeue() { int item = -1; // Assuming -1 represents an empty value if (!isEmpty()) { item = queueArray[front]; if (front == rear) { // Reset front and rear to -1 to indicate an empty queue front = -1; rear = -1; } else { // Circular increment of front index front = (front + 1) % maxSize; } } else { System.out.println("Queue is empty. Cannot dequeue."); } return item; } // Method to peek at the front element of the circular queue without removing it public int peek() { if (!isEmpty()) { return queueArray[front]; } else { System.out.println("Queue is empty. No peek value."); return -1; // Assuming -1 represents an empty value } } // Method to check if the circular queue is empty public boolean isEmpty() { return front == -1 && rear == -1; } // Main method for testing the CircularQueue class public static void main(String[] args) { CircularQueue circularQueue = new CircularQueue(5); circularQueue.enqueue(1); circularQueue.enqueue(2); circularQueue.enqueue(3); // Should print 1 System.out.println("Peek: " + circularQueue.peek()); // Should print 1 System.out.println("Dequeue: " + circularQueue.dequeue()); // Should print 2 System.out.println("Peek after dequeue: " + circularQueue.peek()); } } OutputPeek: 1 Dequeue: 1 Peek after dequeue: 2 Explanation of code:In this code, while inserting instead of inserting element in next space we are incrementing it by one and then performing modulo operation with it.So that it can go to the first element after last element. It results in implementation of circular data structures which uses the memory more efficiently.Complexity of the above Program:Time Complexity:Enqueue operation: O(1)Dequeue operation: O(1)Peek operation: O(1)isEmpty operation: O(1)Space Complexity: O(N) where N is the size of the circular queue. Comment More infoAdvertise with us Next Article How to Implement a Circular Queue in Java ? B bhushanc2003 Follow Improve Article Tags : Java Java Programs java-queue Java Examples Practice Tags : Java Similar Reads How to Implement Queue in Java using Array and Generics? The queue is a linear data structure that follows the FIFO rule (first in first out). We can implement Queue for not only Integers but also Strings, Float, or Characters. There are 5 primary operations in Queue: enqueue() adds element x to the front of the queuedequeue() removes the last element of 4 min read How to Implement a FIFO (First-In-First-Out) Queue in Java? FIFO stands for First In, First Out, which is a common way of organizing and manipulating Data Structure. In FIFO, the first element that is added to the data structure is the same first element that is to be removed first. Queue is a FIFO-based Data Structure. Here, the insert operation is called E 3 min read Java Program to Implement Circular Buffer When data is constantly moved from one place to another or from one process to another or is frequently accessed, it cannot be stored in permanent memory locations such as hard drives as they take time to retrieve the data. This type of data needs to be accessed quickly and is stored in temporary me 8 min read Implementation of a Circular Resizable Array in Java A Circular Resizable Array is a data structure that effectively maintains a fixed-size array by enabling members to be added or withdrawn circularly. It is sometimes referred to as a circular buffer or ring buffer. This cyclical behavior is especially helpful in situations when a smooth array wrap i 4 min read How to Iterate over a Queue in Java? Queue is a concept of Linear Data Structure that follows the concept of FIFO(First In First Out). Queues are majorly used to maintain the order of the elements to which they are added. In this article, we will learn to perform Queue Iteration in Java. Queue Iteration Program in JavaSyntax with Examp 1 min read Like