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;
}
}
Written by Marius Cotofana
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#C
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#