gpt4 book ai didi

C 从链表中删除节点

转载 作者:太空狗 更新时间:2023-10-29 17:10:36 26 4
gpt4 key购买 nike

如何从链表中删除节点?

这是我的代码:

void RemoveNode(Node * node, Node ** head) {
if (strcmp(node->state, (*(*head)->next).state) == 0) {
Node * temp = *head;
*head = (*head)->next;
free(temp);
return;
}

Node * current = (*head)->next;
Node * previous = *head;
while (current != NULL && previous != NULL) {
if (strcmp(node->state, (*current->next).state) == 0) {
Node * temp = current;
previous->next = current->next;
free(temp);
return;
}
current = current->next;
previous = previous->next;
}
return;
}

但我一直遇到段错误。

我觉得我在做一些愚蠢的事情......有什么想法吗?

最佳答案

我的猜测:

void RemoveNode(Node * node, Node ** head) {
if (strcmp(node->state, ((*head)->state) == 0) {
Node * temp = *head;
*head = (*head)->next;
free(temp);
return;
}

Node * current = (*head)->next;
Node * previous = *head;
while (current != NULL && previous != NULL) {
if (strcmp(node->state, current->state) == 0) {
Node * temp = current;
previous->next = current->next;
free(temp);
return;
}
previous = current;
current = current->next;
}
return;
}

关于C 从链表中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com