Circular Singly Linked List in Java | Full Example & Explanation

Circular Singly Linked List in Java | Full Example & Explanation

Circular Singly Linked List in Java – Full Example with Step-by-Step Explanation

Circly linked list

Introduction

A Circular Singly Linked List (CSLL) is similar to a Singly Linked List, but the next pointer of the last node points back to the head node. This creates a circular structure, allowing continuous traversal without null termination.

Java Code with Step-by-Step Explanation


// Node class for CSLL
class Node {
    int data;   // Stores node value
    Node next;  // Reference to next node

    Node(int data){
        this.data = data;
        this.next = null;
    }
}
    
Step: Define a node with data and next. In CSLL, next of the last node will point back to the head.

// Circular Singly Linked List class
class CircularSinglyLinkedList {
    Node head; // start of the list

    // Insert at end
    public void insertAtEnd(int data){
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
            newNode.next = head; // Point to itself
            return;
        }
        Node temp = head;
        while(temp.next != head){ // Traverse until last node
            temp = temp.next;
        }
        temp.next = newNode; // Last node points to new node
        newNode.next = head; // New node points back to head
    }

    // Insert at beginning
    public void insertAtBeginning(int data){
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
            newNode.next = head;
            return;
        }
        Node temp = head;
        while(temp.next != head){
            temp = temp.next; // Go to last node
        }
        newNode.next = head; // New node points to head
        temp.next = newNode; // Last node points to new node
        head = newNode;      // Update head
    }

    // Display list
    public void display(int count){
        if(head == null) return;
        Node temp = head;
        System.out.print("Circular List: ");
        for(int i=0;i
Step: Methods to insert at beginning/end maintain circular links. Display method uses a limit to prevent infinite traversal while showing the circular nature.

// Main class to test CSLL
public class Main {
    public static void main(String[] args){
        CircularSinglyLinkedList csll = new CircularSinglyLinkedList();

        csll.insertAtEnd(10);
        csll.insertAtEnd(20);
        csll.insertAtBeginning(5);
        csll.insertAtEnd(30);

        csll.display(10); // Display 10 nodes max to show circular nature
    }
}
    
Step: Test the CSLL by inserting nodes and displaying. The display shows repeated traversal possible due to circular structure.

Conclusion

The Circular Singly Linked List allows continuous traversal. Unlike Singly Linked List, the last node points back to head, which is useful for applications like round-robin scheduling. Mastering CSLL is the next step after learning SLL and DLL.

Comments

Popular posts from this blog

Software Engineering Explained: Definition, Process Models, and Challenges

Java Basics – What, Why, and the Real Story

Beginner's Guide to the World Wide Web: Websites, Domains, Networks & More!