删除链表节点的二种方法比较
2013-01-12 10:46
211 查看
可以比较下二种方法实现的差异,你更喜欢哪种呢?
方法一:
typedef struct node
{
struct node * next;
....
} node;
typedef bool (* remove_fn)(node const * v);
// Remove all nodes from the supplied list for which the
// supplied remove function returns true.
// Returns the new head of the list.
node * remove_if(node * head, remove_fn rm)
{
for (node * prev = NULL, * curr = head; curr != NULL; )
{
node * next = curr->next;
if (rm(curr))
{
if (prev)
prev->next = curr->next;
else
head = curr->next;
free(curr);
}
else
prev = curr;
curr = next;
}
return head;
}
方法二:
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;
}
}
方法一:
typedef struct node
{
struct node * next;
....
} node;
typedef bool (* remove_fn)(node const * v);
// Remove all nodes from the supplied list for which the
// supplied remove function returns true.
// Returns the new head of the list.
node * remove_if(node * head, remove_fn rm)
{
for (node * prev = NULL, * curr = head; curr != NULL; )
{
node * next = curr->next;
if (rm(curr))
{
if (prev)
prev->next = curr->next;
else
head = curr->next;
free(curr);
}
else
prev = curr;
curr = next;
}
return head;
}
方法二:
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;
}
}
相关文章推荐
- C++删除链表中间节点的方法
- [Happy DSA] 删除单链表中任意一个节点的方法
- 给定链表中间节点指针,删除中间节点的方法
- python实现单链表中删除倒数第K个节点的方法
- 给定链表中间节点指针,删除中间节点的方法
- 编程之美-从无头单链表中删除节点方法整理
- 遍历Linux kernel的链表时删除节点的方法 list_for_each_safe
- PHP实现双链表删除与插入节点的方法示例
- 删除链表中倒数第n个节点
- 删除一个没有头节点链表中的某个节点
- 删除链表中倒数第K个节点,
- 收藏,Linus Torvalds删除链表节点的NB代码。
- 删除链表中重复的节点
- javascript删除一个html元素节点的方法
- 从链表的末端开始,删除第n个节点
- Distinct删除重复数据时 自定义的方法比较【转】
- (算法)输出单链表的倒数第k个节点,删除特点的节点和从链表尾一次输出节点值
- 删除单向链表的部分节点
- jQuery插件zTree实现删除树子节点的方法示例
- 删除链表中的中间节点和 a/b 处的结点 Python 版