act4la
Last Updated: February 25, 2016
·
241
· cmarius02

Removing nodes using pointers

Here is a nice way to remove a node from a singly-linked list:

void remove_if(node ** head, remove_fn rm) {
    for (node** curr = head; *curr; ) {
        node * entry = *curr;
        if (rm(entry)) {
            *curr = entry->next;
            free(entry);
        }
        else
            curr = &entry->next;
    }
}