Double linked list with header node

Sure, here's an example implementation of a doubly linked list with a header node in C:


```
#include <stdio.h>
#include <stdlib.h>

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

// Header structure
struct Header {
    int count;
    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.count = 0;
    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 = NULL;

    if (header.first != NULL) {
        header.first->prev = new_node;
    } else {
        header.last = new_node;
    }
    header.first = new_node;
    header.count++;
}

// 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");
}

// Function to delete a node from the list
void deleteNode(int data) {
    struct Node* current = header.first;
    while (current != NULL) {
        if (current->data == data) {
            if (current == header.first) {
                header.first = current->next;
                if (header.first != NULL) {
                    header.first->prev = NULL;
                }
            } else if (current == header.last) {
                header.last = current->prev;
                if (header.last != NULL) {
                    header.last->next = NULL;
                }
            } else {
                current->prev->next = current->next;
                current->next->prev = current->prev;
            }
            free(current);
            header.count--;
            return;
        }
    current = current->next;
    }
}

int main() {
    initHeader();
    addNode(1);
    addNode(2);
    addNode(3);
    printf("Forward traversal: ");
    traverseForward();
    printf("Backward traversal: ");
    traverseBackward();
    deleteNode(2);
    printf("After deleting 2: ");
    traverseForward();
    return 0;
}
```

In this implementation, the `Header` structure contains a `count` field to keep track of the number of nodes in the list, as well as `first` and `last` fields to point to the first and last nodes in the list. The `initHeader` function initializes these fields to their null values.

The `addNode` function creates a new node with the given data, sets its `next` pointer to the current first node, and sets its `prev` pointer to null. If the list is not empty, it also updates the `prev` pointer of the current first node to point to the new node. Finally, it updates the `first` pointer of the header node to point to the new node and increments the `count` field.



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