开发者

Can't delete the node from the end of a linked list

I have a problem about deleting the last node of my linked list. I put printf functions to check and locate the error but I can't find it. This my code for deleting the last node from an unsorted linked list. At the end you will see four add functions and they are working successfully. The only broken part is deleting from the end.

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 = 开发者_C百科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 );
}


it seems you forgot temp->pre->next = NULL;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜