【发布时间】:2018-11-06 00:39:52
【问题描述】:
我正在编写一个程序,并且在其中一个函数中我必须从 LinkedList 中删除一个值
struct node *delete_val(int value, struct node *head) {
struct node *h1 = head;
if (head == NULL) { return NULL;}
if (head->next == NULL) { free(head); return NULL;}
while (h1 != NULL) {
if (h1->next != NULL && h1->next->data == value){
h1->next = h1->next->next;
} else {
h1 = h1->next;
}
free(h1);
return head;
}
}
如果通过:
(4,[3,4,5,6,4,4,7]);
函数应该返回:
[3,5,6,4,4,7]
但我的函数出现错误:
错误:您的函数返回的列表无效。 节点0的next字段无效(0xf5400650)。
我基本上是在检查下一个节点是否在其“数据”(head->next->data)中包含匹配值,如果是,我将重新切换当前链表的指针(head->next) 到它之后的那个 (head->next->next) 但我什至无法让它工作。
【问题讨论】:
标签: c linked-list