Doubly linked list

1.Introduction

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

2.Delete node in doubly linked list

void delete_node(struct node** headRef, int data)
{
    assert(headRef != NULL);
    struct node* head = *headRef;
    while (head != NULL && head->data != data)
    {
        head = head->next;
    }
    if (head == NULL)
    {
        return;
    }
    
    if (head->prev != NULL)
    {
        head->prev->next = head->next;
    }
    else
    {
        *headRef = head->next;
    }
    
    if (head->next != NULL)
    {
        head->prev->next = head->next;
    }
    
    head->prev = NULL;
    head->next = NULL;
    free(head);
}

Last updated