C 从列表中删除节点时出现问题
从单链接列表中删除节点时遇到问题。尝试删除第一个节点时没有。它似乎失去了对列表其余部分的参考。 有人能帮忙吗? 这就是所讨论的函数,它取列表的头和要删除的节点的板C 从列表中删除节点时出现问题,c,data-structures,linked-list,C,Data Structures,Linked List,从单链接列表中删除节点时遇到问题。尝试删除第一个节点时没有。它似乎失去了对列表其余部分的参考。 有人能帮忙吗? 这就是所讨论的函数,它取列表的头和要删除的节点的板 Car * removeCarFromList(Car * list,char plate[]){ Car * current = list; Car * previous = NULL; while (current != NULL){ if (strcmp(current->plate
Car * removeCarFromList(Car * list,char plate[]){
Car * current = list;
Car * previous = NULL;
while (current != NULL){
if (strcmp(current->plate,plate)==0&& previous == NULL){
list = current -> next;
current -> next = NULL;
return current;
break;
} else if (strcmp(current->plate,plate)==0&& previous != NULL){
previous -> next = current -> next;
current -> next = NULL;
return current;
break;
}
previous = current;
current = current -> next;
}
return NULL;
}
如果要从列表中删除第一个元素,则必须列出的任何指针现在都无效。设置
list=current->next时代码>list的值是函数的本地值,它永远不会返回函数以更新传递给函数的指针作为列表的开始
要做您想做的事情,您需要一个指向指针的指针
Car*removeCarFromList(Car**list,char plate[])
然后将当前更改为:Car*current=*list代码>
然后将list=current->next
更改为*list=current->next代码>
然后在调用函数时,将removeCarFromlist(myPointer,….
更改为removeCarFromlist(&myPointer,….
当删除第一项时,这将改变我的指针指向列表中的下一项。一旦从函数返回,就不会返回到同一函数的下一条指令,因此在返回后使用中断是没有意义的。首先,去掉返回后的中断语句ode>
函数处理链表节点删除必须单独处理头节点的删除,确保新的头
更新到下一个节点
Node *tmp = *head;
*head = tmp->next;
free(tmp);
列表中的第一个节点是列表地址。更改第一个节点时,还必须更改存储为列表地址的地址。因此,您有两个选项:1)传递列表中第一个节点的地址,以便在函数中为其分配新的第一个节点地址,或2)从函数返回新的列表地址,并将该地址指定为调用者中的新列表地址。谢谢!此外,我还试图根据汽车的种类对我的列表进行冒泡排序,从最低到最高。你能帮忙吗?