A header linked list is a type of linked list that includes a header node at the beginning of the list. The header node does not contain any data, but it is used to simplify some operations on the list, such as insertion and deletion.
Here's an example of a header linked list node structure in C:
```
struct Node {
int data;
struct Node* next;
};
struct HeaderNode {
int count; // number of nodes in the list
struct Node* head; // pointer to the first node in the list
};
```
In this code, we have a `HeaderNode` structure that contains two fields: `count`, which represents the number of nodes in the list, and `head`, which is a pointer to the first node in the list. We also have a `Node` structure that represents a single node in the list, with a `data` field and a `next` pointer that points to the next node in the list.
To insert a node into a header linked list, we first create a new node and set its `next` pointer to point to the current first node in the list. We then update the `head` pointer of the header node to point to the new node, and increment the `count` field. Here's an example of how to insert a node into a header linked list in C:
```
void insertNode(struct HeaderNode* header, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = header->head;
header->head = newNode;
header->count++;
}
```
To delete a node from a header linked list, we first search for the node to be deleted by traversing the list. Once we find the node, we update the `next` pointer of the previous node to point to the node after the one we are deleting. We then free the memory used by the deleted node and decrement the `count` field. Here's an example of how to delete a node from a header linked list in C:
```
void deleteNode(struct HeaderNode* header, int data) {
struct Node* current = header->head;
struct Node* previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current != NULL) {
if (previous == NULL) {
header->head = current->next;
} else {
previous->next = current->next;
}
free(current);
header->count--;
}
}
```
In this code, we first traverse the list to find the node with the specified data value. We keep track of the previous node as we traverse the list, so that we can update its `next` pointer after we delete the node. If we find the node to be deleted, we update the `next` pointer of the previous node to point to the node after the one we are deleting. We then free the memory used by the deleted node and decrement the `count` field. Note that we need to handle the case where the node to be deleted is the first node in the list separately, since there is no previous node in that case.
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