gpt4 book ai didi

无法从链表末尾删除节点

转载 作者:行者123 更新时间:2023-11-30 14:28:28 26 4
gpt4 key购买 nike

我在删除链表的最后一个节点时遇到问题。我使用 printf 函数来检查并定位错误,但我找不到它。这是我的代码,用于从未排序的链表中删除最后一个节点。最后您将看到四个添加功能并且它们正在成功运行。唯一损坏的部分是从末尾删除。

void del_Node(LIST* list, int counter) {

if( counter == 0 )
return;

NODE* temp = list->header;

int count=1;

if( list->header == NULL ) {
printf("The list is already EMPTY\n");
return;
}

while( count != counter ) {
temp = temp->next;
count++;
}

printf("%s %s\n", temp->first_name, temp->last_name);

if( list->counter == 1 ) { // Condition for deleting the last node of the linked list

list->header = NULL;
list->tail = NULL;

temp->next = NULL;
temp->pre = NULL;

list->counter--;

}

else if( list->header == temp ) { // Condition for deleting from the beginnig

list->header = temp->next;

printf("%s listenin basından silindi\n", temp->first_name);

temp->next = NULL;
temp->pre = NULL;

list->counter--;

}

else if ( list->tail == temp ) { // Condition for deleting from the end

list->tail = temp->pre;

printf("%s normal listenin tailinden silindi yeni tail %s\n", temp->first_name, temp->pre->first_name);

temp->next = NULL;
temp->pre = NULL;

list->counter--;

}

else { // Condition from deleting from the middle

temp->pre->next = temp->next;

temp->next->pre = temp->pre;

printf("%s normal listede %s------%s arasından silindi\n",temp->first_name, temp->next->first_name, temp->pre->first_name);

temp->next = NULL;
temp->pre = NULL;

list->counter--;

}

del_fn_name(list, temp);
del_ln_name(list, temp);
del_bdt(list, temp);
del_city(list, temp);

free(temp);

printf("List->counter = %d %d %d\n", list->counter, list->fn_count, list->ln_count );
}

最佳答案

你似乎忘记了temp->pre->next = NULL;

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

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