Tra*_*v92 7 c pointers linked-list nodes
如何从链表中删除节点?
这是我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
但我不断遇到seg故障.
我觉得我做的事情很蠢......任何想法?
我猜:
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;
}
Run Code Online (Sandbox Code Playgroud)