Deletion of a node from a double linked list using c

To delete a node from 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. Traverse the list to find the node to be deleted. Keep track of the previous and next nodes.

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

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

Here, `data` is the data value of the node to be deleted. `head` is the pointer to the first node of the list.


3. Update the pointers of the previous and next nodes to remove the node to be deleted.

```
if (current != NULL) {
    if (previous != NULL) {
        previous->next = current->next;
    } else {
        head = current->next;
    }
    if (current->next != NULL) {
        current->next->prev = previous;
    }
    free(current);
} else {
    printf("Node not found in the list.\n");
    return;
}
```

4. Free the memory occupied by the node to be deleted using the `free()` function.


The complete code for deleting a node from 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 deleteNode(struct Node** headRef, int data) {
    struct Node* current = *headRef;
    struct Node* previous = NULL;

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

    if (current != NULL) {
        if (previous != NULL) {
            previous->next = current->next;
        } else {
            *headRef = current->next;
        }
        if (current->next != NULL) {
            current->next->prev = previous;
        }
        free(current);
    } else {
        printf("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;

    deleteNode(&head, 3); // Deleting node with data = 3

    printList(head); // Printing the updated list: 1 2 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