Insert at a specific location In double linked list

To insert a node at a specific location 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 node at the desired position. Keep track of the previous and next nodes.

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

for (int i = 1; i < position && current != NULL; i++) {
    previous = current;
    current = current->next;
}
```

Note that this implementation assumes that the `node` structure has `prev`, `next`, and `data` fields, and that the `head` and `tail` pointers point to the first and last nodes in the list, respectively.

```
newNode->prev = previous;
newNode->next = current;
previous->next = newNode;
current->prev = newNode;
```

Here, `position` is the index at 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 at a specific position 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 insertAtPosition(struct Node** headRef, int data, int position) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;

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

    for (int i = 1; i < position && current != NULL; i++) {
        previous = current;
        current = current->next;
    }

    if (position == 1) {
        newNode->prev = NULL;
        newNode->next = current;
        if (current != NULL) {
            current->prev = newNode;
        }
        *headRef = newNode;
    } else {
        newNode->prev = previous;
        newNode->next = current;
        previous->next = newNode;
        if (current != NULL) {
            current->prev = newNode;
        }
    }
}

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

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

    insertAtPosition(&head, 1, 1); // Inserting 1 at the beginning
    insertAtPosition(&head, 2, 2); // Inserting 2 at position 2
    insertAtPosition(&head, 3, 3); // Inserting 3 at position 3
    insertAtPosition(&head, 4, 4); // Inserting 4 at position 4

    printf("Doubly linked list: ");
    printList(head);

    return 0;
}
```

This code will create a doubly linked list with 4 nodes and print the list as `1 2 3 4`.



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