C# Data Structures - Circular Singly Linked List Other Related Topics
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C# - Delete all nodes of the Circular Singly Linked List



Deleting all nodes of a circular singly linked list requires traverse through the list and deleting each node one by one. It requires creating a current node pointing to the next of head. Delete the current node and move to the next node using temp node. Repeat the process till the current node becomes head. At last, delete the head.

The function deleteAllNodes is created for this purpose. It is a 3-step process.

public void deleteAllNodes() {
  if(this.head != null) {
    
    //1. if head is not null create a temp node
    //   and current node pointed to next of head
    Node temp = new Node();
    Node current = new Node();
    current = this.head.next;
    
    //2. if current node is not equal to head, delete the
    //   current node and move current to next node using temp,
    //   repeat the process till the current reaches the head
    while(current != this.head) {
      temp = current.next;
      current = null;
      current = temp;
    }

    //3. Delete the head
    this.head = null;
  }
  Console.WriteLine("All nodes are deleted successfully."); 
}

The below is a complete program that uses above discussed concept of deleting all nodes of a circular singly linked list which makes the list empty with size zero.

using System;

//node structure
class Node {
  public int data;
  public Node next;
};

class LinkedList {
  Node head;

  public LinkedList(){
    head = null;
  }
  
  //Add new element at the end of the list
  public void push_back(int newElement) {
    Node newNode = new Node();
    newNode.data = newElement;
    newNode.next = null; 
    if(head == null) {
      head = newNode;
      newNode.next = head;
    } else {
      Node temp = new Node();
      temp = head;
      while(temp.next != head)
        temp = temp.next;
      temp.next = newNode;
      newNode.next = head;
    }    
  }

  //delete all nodes of the list
  public void deleteAllNodes() {
    if(this.head != null) {
      Node temp = new Node();
      Node current = new Node();
      current = this.head.next;
      while(current != this.head) {
        temp = current.next;
        current = null;
        current = temp;
      }
      this.head = null;
    }
    Console.WriteLine("All nodes are deleted successfully."); 
  }

  //display the content of the list
  public void PrintList() {
    Node temp = new Node();
    temp = this.head;
    if(temp != null) {
      Console.Write("The list contains: ");
      while(true) {
        Console.Write(temp.data + " ");
        temp = temp.next;
        if(temp == this.head)
          break;        
      }
      Console.WriteLine();
    } else {
      Console.WriteLine("The list is empty.");
    }
  }      
};

// test the code
class Implementation {  
  static void Main(string[] args) {
    LinkedList MyList = new LinkedList();

    //Add four elements in the list.
    MyList.push_back(10);
    MyList.push_back(20);
    MyList.push_back(30);
    MyList.push_back(40);

    //Display the content of the list.
    MyList.PrintList();  

    //delete all nodes of the list
    MyList.deleteAllNodes();

    //Display the content of the list.
    MyList.PrintList();   
  }
}

The above code will give the following output:

The list contains: 10 20 30 40 
All nodes are deleted successfully.
The list is empty.

5