Google News
logo
Java program to delete a node from the beginning of the Circular Linked List
In the following example of Java program to delete a node from the beginning of a circular linked list :
Program :
public class CircularLinkedList {

    // Node class
    static class Node {
        int data;
        Node next;

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

    // Head of the linked list
    Node head;

    // Method to add a node to the end of the list
    public void addLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            head.next = head;
        } else {
            Node current = head;
            while (current.next != head) {
                current = current.next;
            }
            current.next = newNode;
            newNode.next = head;
        }
    }

    // Method to delete the first node of the list
    public void deleteFirst() {
        if (head == null) {
            return;
        } else if (head.next == head) {
            head = null;
        } else {
            Node current = head;
            while (current.next != head) {
                current = current.next;
            }
            current.next = head.next;
            head = head.next;
        }
    }

    public static void main(String[] args) {
        CircularLinkedList list = new CircularLinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);

        System.out.println("Before deleting first node:");
        list.display();

        list.deleteFirst();

        System.out.println("After deleting first node:");
        list.display();
    }

    // Method to display the list
    public void display() {
        if (head == null) {
            return;
        } else {
            Node current = head;
            do {
                System.out.print(current.data + " ");
                current = current.next;
            } while (current != head);
            System.out.println();
        }
    }

}
Output :
Before deleting the first node:
1 2 3 4 
After deleting the first node:
2 3 4 
The above program defines a Node class to represent a node in the circular linked list. The CircularLinkedList class contains a reference to the head of the list, a method to add a node to the end of the list, a method to delete the first node of the list, and a method to display the list.

In the addLast method, a new node is created with the specified data. If the list is empty (i.e. the head is null), the new node becomes the head of the list, and its next field is set to itself, making it a circular list. Otherwise, the method traverses the list starting from the head, and finds the last node. The next field of the last node is set to the new node, and the next field of the new node is set to the head, making it a circular list.

In the deleteFirst method, the method first checks if the list is empty (i.e. the head is null). If it is, the method simply returns without doing anything. If the head is the only node in the list, the head is set to null to empty the list. Otherwise, the method finds the last node of the list, and sets its next field to the second node in the list. The head is then set to the second node in the list.

In the display method, the method first checks if the list is empty (i.e. the head is null). If it is, the method simply returns without doing anything. Otherwise, the method initializes a current node to the head, and traverses the list starting from the head, printing each node's.