我需要从链接列表中删除一个项目。
我愿意通过将标题(称为carbase)重定向到列表中的下一个标题来开始列表的开头。
当我运行该函数时,它确实释放了我的结构数据。但是,指针没有移动。
struct carinfo_t *removeCarinfo(struct carinfo_t *carbase, char *merk, char
*model, int year){
struct carinfo_t *curr = carbase;
while(curr != NULL){
int a, b, c;
a = strcmp(curr->brand, merk);
b = strcmp(curr->model, model);
c = curr->year;
if (a == 0 && b == 0 && c == year){ //verwijderen wanneer dit waar is
if(curr = carbase) //als het de eerste in de lijst is
carbase = carbase->next;
freeCarinfo(curr);
return carbase;
}
curr = curr->next;
}
}
最佳答案
使用这样的方法:
void deleteList(struct Node** head_ref)
{
/* deref head_ref to get the real head */
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
/* deref head_ref to affect the real head back
in the caller. */
*head_ref = NULL;
}
您只需要在此处传递头Node引用。