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) {

//1. if head is not null create a temp node
//   and current node pointed to next of head
struct Node *temp, *current;

//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
temp = current->next;
free(current);
current = temp;
}

}
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;
} else {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

//delete all nodes of the list
struct Node *temp, *current;
temp = current->next;
free(current);
current = temp;
}
}
printf("All nodes are deleted successfully.\n");
}

//display the content of the list
printf("The list contains: ");
while (1) {
printf("%i ",temp->data);
temp = temp->next;
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.
```

5