c list删除结构体中所有等于这个值得元素
时间: 2024-03-23 11:37:31 浏览: 15
假设要删除的元素是结构体中的某个成员变量的值为val,可以使用以下代码:
```c
struct Node {
int val;
struct Node *next;
};
void deleteNodes(struct Node** head_ref, int val) {
struct Node *temp = *head_ref, *prev;
while (temp != NULL && temp->val == val) {
*head_ref = temp->next;
free(temp);
temp = *head_ref;
}
while (temp != NULL) {
while (temp != NULL && temp->val != val) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
return;
}
prev->next = temp->next;
free(temp);
temp = prev->next;
}
}
```
这段代码接受一个指向链表头指针的指针head_ref以及要删除的值val。首先,它会找到链表头部所有值为val的节点并删除它们。接着,它会遍历链表中的所有节点,找到值为val的节点并删除它们。