Deletion of a node after a given node from a double linked list using c

To delete a node after a given node in a doubly linked list in C, you can follow these steps:


1. Check if the list is empty. If it is empty, there is nothing to delete.

2. Traverse the list from the head to the node after the given node.

3. Check if the node after the given node exists. If it doesn't exist, there is nothing to delete.

4. Set the next pointer of the given node to point to the node after the node to be deleted.

5. Set the previous pointer of the node after the node to be deleted to point to the given node.

6. Free the memory occupied by the node to be deleted.


Here's the code implementation:

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

// Function to delete node after given node
void deleteAfter(struct Node* given_node) {
    // Check if list is empty
    if (given_node == NULL) {
        printf("List is empty\n");
        return;
    }

    // Traverse the list to find the node after the given node
    struct Node* node_after = given_node->next;
    if (node_after == NULL) {
        printf("No node to delete after the given node\n");
        return;
    }

    // Set the next pointer of given node to point to node after the node to be deleted
    given_node->next = node_after->next;
    // Set the previous pointer of node after the node to be deleted to point to the given node
    if (node_after->next != NULL) {
        node_after->next->prev = given_node;
    }

    // Free the memory occupied by the node to be deleted
    free(node_after);
}
```

You can call the `deleteAfter()` function with the given node as the parameter to delete the node after the given node in the doubly 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