1. Introduction
A singly linked list is a type of data structure where each node points to the next node in the sequence, with the last node pointing to null. A circular linked list is similar but the last node points back to the first node, creating a closed loop. In this post, we’ll explore how to convert a singly linked list into a circular linked list using Java.
2. Program Steps
1. Define a Node class to represent each element of the linked list.
2. Create a LinkedList class that will hold methods for:
– Adding elements to the end of the list.
– Converting the singly linked list to a circular linked list.
– Displaying the list elements.
3. Implement a driver program to test these methods.
3. Code Program
class Node {
int data; // Node's data
Node next; // Reference to the next node
public Node(int data) { // Constructor to initialize the node
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head; // Starting node of the list
// Method to add a new node at the end
public void append(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
// Method to convert singly linked list to circular linked list
public void convertToCircular() {
if (head == null) return;
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = head; // Connect last node back to head
}
// Utility method to display the list elements
public void display(int count) { // count specifies number of elements to print in loop
Node temp = head;
int i = 0;
while (temp != null && i < count) {
System.out.print(temp.data + " -> ");
temp = temp.next;
i++;
}
System.out.println((temp == head) ? "HEAD" : "NULL");
}
}
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.append(40);
list.append(50);
System.out.println("Singly Linked List:");
list.display(5);
list.convertToCircular();
System.out.println("\nCircular Linked List (first 8 nodes):");
list.display(8);
}
}
Output:
Singly Linked List: 10 -> 20 -> 30 -> 40 -> 50 -> NULL Circular Linked List (first 8 nodes): 10 -> 20 -> 30 -> 40 -> 50 -> 10 -> 20 -> 30 -> HEAD
4. Step By Step Explanation
1. We use the Node class to define the basic structure of each node in our linked list. Every node holds its data and a reference next which points to the subsequent node in the list.
2. The LinkedList class comprises methods for adding nodes to the list (append), converting the singly linked list into a circular one (convertToCircular), and for displaying the list (display). The display method takes an argument count to limit the number of nodes shown, useful for circular lists to avoid infinite loops.
3. The convertToCircular method finds the last node of the singly linked list and adjusts its next pointer back to the head, making the list circular.
4. In our LinkedListDemo, we construct a singly linked list, display it, then convert it to a circular linked list and display it again to showcase the circular nature.
Converting a singly linked list to a circular one is simple yet valuable, especially in contexts where circular iteration is useful.