Search code examples
cwhile-looplinked-listsingly-linked-listfunction-definition

Getting user input for the deletion of the desired linked list element


NodePointer deleteNode(NodePointer head,int number)
{
    NodePointer ptrNode;
    NodePointer ptrNodePrev;
    int count;
    if(head == NULL)
    {
        printf("There is no element at list");
    }
    if(number == 1)
    {
       ptrNodePrev=head;
       head=head->next;
       free(ptrNodePrev);
    }
    else{
        ptrNode=head;
        while(ptrNode->next != NULL && count+1 != number){
            count++;
            ptrNodePrev=ptrNode;
            ptrNode=ptrNode->next;
        }
        ptrNode=ptrNode->next;
        printf("Node: %d-%d-%d - %s",ptrNodePrev->next->x.year,
                                      ptrNodePrev->next->x.month,
                                      ptrNodePrev->next->x.day,
                                      ptrNodePrev->next->x.event);
        printf("Successfully deleted");
        free(ptrNodePrev->next);
        ptrNodePrev->next=ptrNode;
    }
    return head;
}

Hi I've been learning single linked lists and wanted to do a example about it.Basically i ask user to input how many nodes does he want and then he fills them. After that i ask them to delete a node then they will enter a number which will supposedly deletes the according node.Its fine if the number inputed is not 1 but if it is then i would basically get random numbers. I dont get why it happens i looked through most of the questions about this topic here but couldnt find reason.Can anyone explain my mistake please ?


Solution

  • Actually when number is equal to 1 then the function works correctly.

    NodePointer deleteNode(NodePointer head,int number)
    {
        NodePointer ptrNode;
        NodePointer ptrNodePrev;
        int count;
        if(head == NULL)
        {
            printf("There is no element at list");
        }
        if(number == 1)
        {
           ptrNodePrev=head;
           head=head->next;
           free(ptrNodePrev);
        }
        else
        {
            //...
        }
        return head;
    }
    

    Maybe in main you forgot to assign the return value of the function to the pointer to the head node like

    head = deleteNode( head, 1 );
    

    Otherwise the function has undefined behavior because 1) the variable count is not initialized and 2) you do not check that after the while loop count + 1 is equal to number

        while(ptrNode->next != NULL && count+1 != number){
            count++;
            ptrNodePrev=ptrNode;
            ptrNode=ptrNode->next;
        }
        ptrNode=ptrNode->next;
        //...
    

    Pay attention to that in C indices start from 0. And the second parameter of the function should have an unsigned integer type as for example size_t. Otherwise the user is allowed to pass to the function a negative value.

    Also the function should not issue any message.

    The function can be declared and defined the following way.

    int deleteNode( NodePointer *head, size_t n )
    {
        while ( n-- && *head != NULL ) head = &( *head )->next;
    
        int success = *head != NULL;
    
        if ( success )
        {
             NodePointer current = *head;
             *head = ( *head )->next;
             free( current );
        }
    
        return success;
    }
    

    And if in main you have a declaration of the pointer like

    NodePointer head = NULL;
    //...
    

    then the function is called like

    deleteNode( &head, n );
    

    where n is some value specifying an index in the list starting form 0. That is if you want to delete the first node then you should write

    deleteNode( &head, 0 );