Traversing a double linked list

Traversing a double linked list in C can be done using the same basic approach as for a single linked list. The only difference is that we need to move forward or backward depending on whether we want to traverse the list from the beginning or the end. Here is an example of traversing a double linked list in C:


1. Creating a double linked list:
```c
void display() {
struct node *temp = head;
    printf("List: ");

    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }

    printf("\n");
}

void display_reverse() {
    struct node *temp = tail;
    printf("Reverse List: ");

    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->prev;
    }

    printf("\n");
}
```

In the `display` function, we start from the beginning of the list (`head`) and print each node's data while moving forward (`temp = temp->next`). In the `display_reverse` function, we start from the end of the list (`tail`) and print each node's data while moving backward (`temp = temp->prev`). Note that we use the `prev` pointer to move backward in a double linked list.



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