链表删除小技巧

2016-12-14  本文已影响0人  jiango86

CoolShell上看到的小技巧,记下来先。

typedef std::function<bool(const node* n)> remove_fn;
void remove(node** head, remove_fn fn) 
{
    for (node** curr = head; *curr;) {
        node* entry = *curr;
        if (fn(entry)) {
            *curr = entry->next;
            delete entry;
        } else {
            curr = &entry->next;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读