Circular Queue using Linked List in Java

Circular Queue using Linked List

Circular Queue using Linked List in Java is a dynamic implementation of a circular queue where nodes are connected in a circular manner.

Unlike an array based circular queue (which has fixed capacity), a linked list circular queue grows dynamically and avoids overflow until memory is exhausted.

circular queue using linked list

What is a Circular Queue using Linked List?

In a normal linked list queue:

front → 10 → 20 → 30 → null

In a circular linked list queue, the last node points back to the first node:

front → 10 → 20 → 30
           ↑        ↓
           ← ← ← ← ←

The following are the operations that can be performed on a circular queue:

  • Front: It is used to get the front item from the Queue.
  • Rear: It is used to get the last element from the Queue.
  • enQueue() This operation adds a new node after rear and moves rear to the next node.
  • deQueue() This operation removes the front node and moves front to the next node.

Why Use Circular Queue with Linked List?

FeatureCircular ArrayCircular Linked List
SizeFixedDynamic
OverflowYesNo (until memory full)
Memory UseContinuousNon-contiguous
FlexibilityMediumHigh

Use it when:

  1. Queue size is unpredictable
  2. Circular traversal is required
  3. Memory flexibility is needed
circular queue using linked list

Algorithm for Implementation of Circular Queue using Linked List

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Java Code for Circular Queue using Linked List

Run
class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class CircularQueueLinkedList {

    private Node front;
    private Node rear;

    public CircularQueueLinkedList() {
        front = rear = null;
    }

    // Check if queue is empty
    public boolean isEmpty() {
        return front == null;
    }

    // Enqueue operation
    public void enqueue(int value) {

        Node newNode = new Node(value);

        if (isEmpty()) {
            front = rear = newNode;
            rear.next = front; // circular link
        } else {
            rear.next = newNode;
            rear = newNode;
            rear.next = front;
        }

        System.out.println(value + " inserted");
    }

    // Dequeue operation
    public int dequeue() {

        if (isEmpty()) {
            System.out.println("Queue is empty (Underflow)");
            return -1;
        }

        int value;

        // If only one element
        if (front == rear) {
            value = front.data;
            front = rear = null;
        } else {
            value = front.data;
            front = front.next;
            rear.next = front;
        }

        return value;
    }

    // Peek operation
    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty");
            return -1;
        }
        return front.data;
    }

    // Display queue
    public void display() {

        if (isEmpty()) {
            System.out.println("Queue is empty");
            return;
        }

        System.out.print("Circular Queue: ");

        Node temp = front;
        do {
            System.out.print(temp.data + " ");
            temp = temp.next;
        } while (temp != front);

        System.out.println();
    }

    // Main method
    public static void main(String[] args) {

        CircularQueueLinkedList queue = new CircularQueueLinkedList();

        queue.enqueue(10);
        queue.enqueue(20);
        queue.enqueue(30);

        queue.display();

        System.out.println("Removed: " + queue.dequeue());
        System.out.println("Removed: " + queue.dequeue());

        queue.display();

        queue.enqueue(40);
        queue.enqueue(50);

        queue.display();

        System.out.println("Front element: " + queue.peek());

        System.out.println("Removed: " + queue.dequeue());
        System.out.println("Removed: " + queue.dequeue());
        System.out.println("Removed: " + queue.dequeue());

        queue.display();  // underflow case
    }
}

Input:

enqueue(10)
enqueue(20)
enqueue(30)
dequeue()
dequeue()
enqueue(40)
enqueue(50)
dequeue()
dequeue()
dequeue()

Output:

10 inserted
20 inserted
30 inserted
Circular Queue: 10 20 30
Removed: 10
Removed: 20
Circular Queue: 30
40 inserted
50 inserted
Circular Queue: 30 40 50
Front element: 30
Removed: 30
Removed: 40
Removed: 50
Queue is empty

Note:

In some cases like:

  1. Empty queue:
    front == null → prevent dequeue and peek
  2. Single element case:
    After deletion, set both front and rear to null
  3. Circular link maintenance:
    Always update rear.next = front

Frequently Asked Questions

Answer:

It is a queue where the last node connects back to the first node, forming a circular structure.

Answer:

Enqueue and dequeue operations take O(1) time.

Answer:

No fixed overflow, it grows dynamically until memory is exhausted.

Answer:

Array version has fixed size; linked list version is dynamic.

Answer:

To preserve the circular structure of the queue.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java