【发布时间】:2021-06-29 06:23:19
【问题描述】:
我在使用这个函数时遇到了这个问题,我想用给定的字符串删除所有节点,但是当我在我的程序上运行它时,它会从第一个 find char ex[] 中删除所有节点,我是 c 新手指导我
void delSearch(char ex[]){
int del;
struct file *temp = head;
while(temp!=NULL){
if(strcmp(temp->ex,ex)==0){
file *temp2 = temp->next;
file *temp3 = temp;
temp->next=temp2;
free(temp3);
temp3 = NULL;
}
else
{
temp = temp->next;
}
}
}
【问题讨论】:
-
如果您的问题与纯C代码有关,请不要标记C++。
-
temp->next=temp2;等价于temp->next=temp->next;看起来有点多余 -
@Ollie 不清楚是需要删除给定字符串的第一个节点还是给定字符串的所有节点。
-
您需要跟踪前一个节点。这样您就可以执行
prev->next = temp->next;之类的操作来从列表中删除temp节点。您可能正在尝试使用temp->next=temp2;来做到这一点。但这是错误的。它什么也不做,因为它只是temp->next=temp->next(正如上面的评论中已经指出的那样)。 -
@Ollie 我问你一个问题不是吗?
标签: c linked-list singly-linked-list c-strings function-definition