简体   繁体   中英

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 = 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM