Sure, here's an example implementation of a double circular linked list with a header node in C:
```c
#include <stdio.h>
#include <stdlib.h>
// Define a structure for the nodes in the linked list
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Define a function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Define a function to insert a new node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
newNode->prev = newNode;
newNode->next = newNode;
*head = newNode;
} else {
newNode->prev = (*head)->prev;
newNode->next = *head;
(*head)->prev->next = newNode;
(*head)->prev = newNode;
*head = newNode;
}
}
// Define a function to insert a new node at the end of the linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
newNode->prev = newNode;
newNode->next = newNode;
*head = newNode;
} else {
newNode->prev = (*head)->prev;
newNode->next = *head;
(*head)->prev->next = newNode;
(*head)->prev = newNode;
}
}
// Define a function to print the linked list
void printList(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
} else {
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
}
// Define a function to delete a node from the linked list
void deleteNode(struct Node** head, int data) {
if (*head == NULL) {
return;
}
struct Node* current = *head;
while (current->data != data && current->next != *head) {
current = current->next;
}
if (current->data == data) {
if (current == *head) {
*head = (*head)->next;
}
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
}
}
// Define a function to delete the entire linked list
void deleteList(struct Node** head) {
if (*head == NULL) {
return;
}
struct Node* current = *head;
do {
struct Node* temp = current;
current = current->next;
free(temp);
} while (current != *head);
*head = NULL;
}
// Demonstration of the linked list functions
int main() {
struct Node* head = NULL;
// Insert nodes at the beginning of the list
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
// Print the list
printf("List after inserting nodes at the beginning: ");
printList(head);
// Insert nodes at the end of the list
insertAtEnd(&head, 4);
insertAtEnd(&head, 5);
insertAtEnd
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