Header lined list

In a header linked list, a dummy node called a header node is added at the beginning of the list. The header node does not contain any data, but it serves as a placeholder to simplify the implementation of certain operations. In a double linked list, each node has two pointers, one pointing to the next node and the other pointing to the previous node.


To implement a header 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 structure for the header node, which should contain a pointer to the first node in the list and a pointer to the last node in the list.

3. Create a function to initialize the header node with null pointers for the first and last nodes.

4. 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.

5. In the add function, set the next pointer of the new node to point to the first node in the list, set the previous pointer of the new node to point to the header node, and update the next and previous pointers of the surrounding nodes to include the new node. If the list is empty, update the first and last pointers of the header node to point to the new node.

6. Create a function to traverse the list in forward direction, starting from the first node in the list, and print the data in each node.

7. Create a function to traverse the list in backward direction, starting from the last node in the list, and print the data in each node.

8. 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 first or last node, update the first or last pointer of the header node accordingly.


Here's an example implementation of a header linked list in C:

```
// Node structure
struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
};

// Header structure
struct Header {
    struct Node* first;
    struct Node* last;
};

// Global variable for header of the list
struct Header header;

// Function to initialize the header node
void initHeader() {
    header.first = NULL;
    header.last = 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 = header.first;
    new_node->prev = &header;
    if (header.first != NULL) {
        header.first->prev = new_node;
    }
    header.first = new_node;
    if (header.last == NULL) {
        header.last = new_node;
    }
}

// Function to traverse the list in forward direction and print the data in each node
void traverseForward() {
    struct Node* current = header.first;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// Function to traverse the list in backward direction and print the data in each node
void traverseBackward() {
    struct Node* current = header.last;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->prev;
    }
    printf("\n");
}


About the Author



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





 PreviousNext