Remove Node with Key

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

void removeNodeWithData(struct node **head, int removeData)
{
    struct node *current = *head;
    struct node *prev=*head;
    while(current->data == removeData) //head
    {
        *head = (*head)->next;
        free(current);
        current = *head;
    }

    prev = *head;
    current = (*head)->next;
    while(current != NULL)
    {
        if(current->data == removeData)
        {
            prev->next = current->next;
            free(current);
        }
        else{
            prev = current;

        }
        current = prev->next;
    }

}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...