从链表中删除节点(修改问题)
struct node *delete_from_list(struct node *list, int n){
struct node *cur, *prev;
for (cur = list, prev = NULL;
cur != NULL && cur->value != n;
prev = cur, cur = cur->next)
;
if (cur == NULL)
return list;
if (prev ==NULL)
list = list->next;
else
prev->next = cur->next;
free(cur);
return list;
}修改使用一个指针变量而不是两个(即cur和prev)
修改后如下:
struct node *delete_from_list(struct node **list, int n) //**list为指向需要修改的list指针的指针
{
struct node *cur;
while ((cur = (*list) != NULL && cur->value < new_node->value)
*list = cur->list;
if (cur == NULL)
return *list;
else
*list = cur->list;
free(cur);
return *list;
}
修改为此,正确吗?