无法从链接列表的末尾删除节点

时间:2011-05-12 23:42:04

标签: c list data-structures linked-list

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

1 个答案:

答案 0 :(得分:2)

好像你忘记了temp->pre->next = NULL;