Skip to content

Instantly share code, notes, and snippets.

@jiyeqian
Last active December 15, 2015 06:58
Show Gist options
  • Save jiyeqian/5219663 to your computer and use it in GitHub Desktop.
Save jiyeqian/5219663 to your computer and use it in GitHub Desktop.
remove the node from a list (http://coolshell.cn/articles/8990.html)
void remove_if ( node **head, node **rear, remove_fn rm )
{
for ( node **curr = head; *curr; ) {
node *entry = *curr;
if ( rm ( entry ) ) {
*curr = entry->next;
if ( *curr ) {
(*curr)->previous = entry->previous;
} else { // the rear node removed
*rear = entry->previous;
}
free ( entry );
} else {
curr = &entry->next;
}
}
}
// http://coolshell.cn/articles/8990.html
void remove_if ( node **head, remove_fn rm )
{
for ( node **curr = head; *curr; ) {
node *entry = *curr;
if ( rm ( entry ) ) {
*curr = entry->next; // remove
free ( entry );
} else {
curr = &entry->next; // move
}
}
}
@jiyeqian
Copy link
Author

remove:

*curr = entry->next;

move:

curr = &entry->next;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment