A circular doubly linked list is a type of linked list in which the last node points to the first node, creating a circular structure. Each node has two pointers, one pointing to the next node and the other pointing to the previous node.
To implement a circular doubly linked list in C, you can follow these steps:
1. Create a structure for the nodes of the list, which should contain a data field, a pointer to the next node, and a pointer to the previous node.
2. Create a function to add a new node to the list. This function should take the data to be added as a parameter and should allocate memory for the new node.
3. In the add function, set the next and previous pointers of the new node to point to the head and tail of the list, respectively. If the list is empty, set the head and tail pointers to the new node.
4. Create a function to traverse the list in forward direction, starting from the head node, and print the data in each node.
5. Create a function to traverse the list in backward direction, starting from the tail node, and print the data in each node.
6. Create a function to delete a node from the list. This function should take the node to be deleted as a parameter and should update the next and previous pointers of the surrounding nodes to maintain the integrity of the list. If the node being deleted is the head or tail node, update the head or tail pointer accordingly.
Here's an example implementation of a circular doubly linked list in C:
```
// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Global variables for head and tail of the list
struct Node* head = NULL;
struct Node* tail = NULL;
// Function to add a node to the list
void addNode(int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = head;
new_node->prev = tail;
if (head == NULL) {
head = new_node;
}
if (tail != NULL) {
tail->next = new_node;
}
tail = new_node;
}
// Function to traverse the list in forward direction and print the data in each node
void traverseForward() {
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
// Function to traverse the list in backward direction and print the data in each node
void traverseBackward() {
struct Node* current = tail;
do {
printf("%d ", current->data);
current = current->prev;
} while (current != tail);
printf("\n");
}
// Function to delete a node from the list
void deleteNode(struct Node* node) {
if (node == head) {
head = node->next;
}
if (node == tail) {
tail = node->prev;
}
node->prev->next = node->next;
node->next->prev = node->prev;
free(node);
}
```
You can call the `addNode()`, `traverseForward()`, `traverseBackward()`, and `deleteNode()` functions to add nodes to the list, traverse the list, and delete nodes from the list.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc