C Data Structures - Circular Doubly Linked List Other Related Topics

C - Delete all nodes of the Circular Doubly Linked List



Deleting all nodes of a circular doubly 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.

void deleteAllNodes(struct Node** head_ref) {
  if(*head_ref != NULL) {
    
    //1. if head is not null create a temp node
    //   and current node pointed to next of head
    struct Node *temp, *current;
    current = (*head_ref)->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 != *head_ref) {
      temp = current->next;
      free(current);
      current = temp;
    }

    //3. Delete the head
    free(*head_ref);
    *head_ref = NULL;
  }
  printf("All nodes are deleted successfully.\n");  
}

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

#include <stdio.h>
#include <stdlib.h>

//node structure
struct Node {
  int data;
  struct Node* next;
  struct Node* prev;
};

//Add new element at the end of the list
void push_back(struct Node** head_ref, int newElement) {  
  struct Node *newNode, *temp;
  newNode = (struct Node*)malloc(sizeof(struct Node)); 
  newNode->data = newElement;  
  newNode->next = NULL;
  newNode->prev = NULL;
  if(*head_ref == NULL) {
    *head_ref = newNode;
     newNode->next = *head_ref;
     newNode->prev = *head_ref;
  } else {
    temp = *head_ref;
    while(temp->next != *head_ref) {
      temp = temp->next;
    }    
    temp->next = newNode;
    newNode->next = *head_ref;
    newNode->prev = temp;
    (*head_ref)->prev = newNode;
  }
}

//delete all nodes of the list
void deleteAllNodes(struct Node** head_ref) {
  if(*head_ref != NULL) {
    struct Node *temp, *current;
    current = (*head_ref)->next;
    while(current != *head_ref) {
      temp = current->next;
      free(current);
      current = temp;
    }
    free(*head_ref);
    *head_ref = NULL;
  }
  printf("All nodes are deleted successfully.\n");  
}

//display the content of the list
void PrintList(struct Node* head_ref) {
  struct Node* temp = head_ref;
  if(head_ref != NULL) {
    printf("The list contains: ");
    while (1) {
      printf("%i ",temp->data);
      temp = temp->next;
      if(temp == head_ref)
        break;    
    }
    printf("\n");
  } else {
    printf("The list is empty.\n");
  }   
}

// test the code 
int main() {
  struct Node* MyList = NULL;

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

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

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

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

  return 0; 
}

The above code will give the following output:

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