Here's an example implementation of a single linked list with a header node in C:
```
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct HeaderNode {
int count;
struct Node* head;
};
void initializeList(struct HeaderNode* header) {
header->count = 0;
header->head = NULL;
}
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++;
}
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--;
}
}
void printList(struct HeaderNode* header) {
struct Node* current = header->head;
printf("List contents: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct HeaderNode header;
initializeList(&header);
insertNode(&header, 5);
insertNode(&header, 7);
insertNode(&header, 3);
printList(&header);
deleteNode(&header, 7);
printList(&header);
return 0;
}
```
In this code, we define 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. We also define a `HeaderNode` structure that contains a `count` field representing the number of nodes in the list, and a `head` pointer that points to the first node in the list.
The `initializeList` function initializes a header node to an empty list with `count` set to 0 and `head` set to `NULL`.
The `insertNode` function inserts a new node with the specified data value at the beginning of the 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.
The `deleteNode` function deletes the first node with the specified data value from the list. We traverse the list to find the node with the specified data value, keeping track of the previous node as we go. 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.
The `printList` function prints the data values of all nodes in the list.
In the `main` function, we initialize a header node to an empty list, insert three nodes into the list, print the contents of the list, deletse a node from the list, and print the contents of the updated 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