Insert after a given node In double linked list

To insert a node after a given node in a doubly linked list using C, you can follow the steps below:


1. Define a structure for the doubly linked list node that includes data and pointers to the previous and next nodes.

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

2. Create a new node to be inserted, and set its data value.

```
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
```

3. Traverse the list to find the given node. Keep track of the previous and next nodes.

```
struct Node* current = head;
struct Node* previous = NULL;

while (current != NULL && current->data != givenData) {
    previous = current;
    current = current->next;
}
```

4. Update the pointers of the new node, the given node, and the next node to insert the new node after the given node.

```
newNode->prev = current;
newNode->next = current->next;
current->next = newNode;
if (newNode->next != NULL) {
    newNode->next->prev = newNode;
}
```

Here, `givenData` is the data value of the node after which you want to insert the new node. `head` is the pointer to the first node of the list.


The complete code for inserting a node after a given node in a doubly linked list using C is shown below:

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

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

void insertAfterNode(struct Node** headRef, int givenData, int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;

    struct Node* current = *headRef;
    struct Node* previous = NULL;

    while (current != NULL && current->data != givenData) {
        previous = current;
        current = current->next;
    }

    if (current != NULL) {
        newNode->prev = current;
        newNode->next = current->next;
        current->next = newNode;
        if (newNode->next != NULL) {
            newNode->next->prev = newNode;
        }
    } else {
        printf("Given node not found in the list.\n");
        return;
    }
}

void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main() {
    struct Node* head = NULL;

    // Creating a doubly linked list: 1<->2<->3<->4
    struct Node* node1 = (struct Node*)malloc(sizeof(struct Node));
    struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));
    struct Node* node3 = (struct Node*)malloc(sizeof(struct Node));
    struct Node* node4 = (struct Node*)malloc(sizeof(struct Node));

    node1->data = 1;
    node1->prev = NULL;
    node1->next = node2;

    node2->data = 2;
    node2->prev = node1;
    node2->next = node3;

    node3->data = 3;
    node3->prev = node2;
    node3->next = node4;

    node4->data = 4;
    node4->prev = node3;
    node4->next = NULL;

    head = node1;

    insertAfterNode(&head, 2, 5); //
}


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