2011-05-12 145 views
0

我有一个关于删除链接列表的最后一个节点的问题。我把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); 
} 

回答

2

看来你忘了temp->pre->next = NULL;

+0

我只是从其他删除功能的认可,并准备写“对不起队友我固定”。我为这个愚蠢的错误花了几个小时。 :)。感谢队友的帮助。 – quartaela 2011-05-12 23:58:23

+0

@ user743898:另外请注意,你似乎忘了删除开头:temp-> next-> pre =(something)...说实话,我没有看到从中间删除,但我会如果我是你的话,看看它。祝你好运:) – amit 2011-05-13 00:01:28

+0

是的另一个错误,你是对的:D。感谢您的帮助和建议:D – quartaela 2011-05-13 00:03:50