Circular Linked List Implementation in Java

The Circular Linked List (CLL) is an extension of the traditional singly linked list. In a CLL, the last node points back to the first node. This circular structure facilitates several operations where one might need to loop around the list continuously, such as in applications like round-robin scheduling. 

In this article, we will learn how to implement a Circular Linked List in Java.

Circular Linked List Implementation in Java

Node Structure of Circular Linked List: 

At the core, a node in a CLL is very similar to that of a singly linked list.

class Node {
    int data;
    Node next;

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

Circular Linked List Implementation:

class CircularLinkedList {
    private Node head;
    private Node tail;

    public CircularLinkedList() {
        head = null;
        tail = null;
    }

    // Inserting a node at the end of the list
    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
            tail.next = head;
            return;
        }
        tail.next = newNode;
        tail = newNode;
        tail.next = head;
    }

    // Inserting a node at the beginning of the list
    public void prepend(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        tail.next = newNode;
        head = newNode;
    }

    // Deleting a node with specific value
    public void delete(int data) {
        if (head == null) return;

        Node current = head;
        Node prev = null;

        // If head node itself holds the data to be deleted
        if (head.data == data) {
            head = head.next;
            tail.next = head;
            return;
        }

        // Searching for the data to be deleted
        while (current.data != data) {
            if (current.next == head) return;
            prev = current;
            current = current.next;
        }

        prev.next = current.next;
    }

    // Displaying the circular linked list
    public void display() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }

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

        System.out.println("HEAD");
    }
}

Testing the Circular Linked List Implementation:

public class Main {
    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();

        cll.append(10);
        cll.append(20);
        cll.append(30);
        cll.prepend(5);

        cll.display();  // 5 -> 10 -> 20 -> 30 -> HEAD

        cll.delete(20);
        cll.display();  // 5 -> 10 -> 30 -> HEAD
    }
}

Output:

5 -> 10 -> 20 -> 30 -> HEAD
5 -> 10 -> 30 -> HEAD

Benefits of Circular Linked List

Continuous Looping: CLL allows you to navigate the list in a loop, which can be beneficial in cyclic data scenarios. 

Simplified Operations: Due to its circular nature, some operations become simpler compared to a regular linked list, especially when dealing with the end of the list. 

Conclusion

The Circular Linked List, with its ability to wrap around, offers unique functionalities that are often required in various computational problems. Through Java, its implementation becomes a tangible tool for developers to not only understand its intricacies but also to utilize its features in real-world applications. Its potential use cases range from computer algorithms like round-robin to even simpler tasks like a playlist shuffle. Mastering CLL aids in understanding more complex data structures and the algorithms associated with them.

Comments