-1

I was assigned to delete a number entered by user from a doubly linked list. So my code is successfully finding the number in the linked list and checking whether the linked list is empty or not but it is not able to delete it. It deletes the whole linked list instead of the number itself. I am unable to find the mistake. So kindly help me as I am literally stuck here. Here is my code:

void deleteSpcificValue(int val)
{
    int result = 0;
    Node *curr = head;
    if(head == nullptr)
    {
        curr = head;
        cout << "List Is Empty"<<endl;
    }
    else
    {
        while(curr != nullptr)
        {
            if(curr -> data == val)
            {
               cout << "Value Deleted" << endl;
                result = 1;
                curr = nullptr;
                delete curr;
            }
            curr = curr -> next;
        }
    }
   if(result != 1)
    {
        cout << "The Value To Be Deleted Is Not Found" <<endl;
    }

}
8
  • To delete a given node from a doubly linked list, you need to make the previous node point to the next node, and you need to make the next node point to the previous node. Otherwise the nodes in your list are still going to be pointing to the node you have just deleted. There is none of that logic in the code above. Commented Sep 16, 2022 at 18:51
  • curr = nullptr; followed by delete curr; doesn't make a bunch of sense. Also curr = curr->next; immediately after either curr = nullptr; or delete curr; is a bad idea. Commented Sep 16, 2022 at 18:51
  • Also once you have found the node you want to delete, the while loop should also stop. Again this logic is not present. You need to think carefully about precisely what the code need to do, and also what the code you written actually does. Just writing some code that is sort of correct is not going to work. If it helps get a pen and paper and draw out the linked list with boxes and arrows, to understand exactly what you need to do. Commented Sep 16, 2022 at 18:55
  • 1
    IMHO, when dealing with linked lists, operations should also be drawn with pen and paper; each step. Commented Sep 16, 2022 at 20:10
  • 1
    Here just to second @ThomasMatthews. When playing with linked lists or trees or anything of the sort, it is always worth your time to get out some crayons and construction paper. (And maybe even yarn and a tackboard. Have fun!) The trick is to never let a “node” exist without a line (piece of yarn?) connecting to it from somewhere. Commented Sep 16, 2022 at 20:45

1 Answer 1

1

The function does not make sense because it does not update data members next and prev (I assume that you have the name prev or something similar for the pointer that points to the previous node)

Also it does not delete the found node because you set it to nullptr

curr = nullptr;
delete curr;

Also as after this code snippet the pointer curr is equal to nullptr then the following statement

curr = curr -> next;

invokes undefined behavior.

And if the deleted node is the head node then your function does not update the pointer to the head node.

Pay attention to that the function should not issue any message. It is the caller of the function that will decide whether to issue a message.

The function can be defined the following way

bool deleteSpcificValue( int val )
{
    Node *current = head;

    while ( current != nullptr && current->data != val )
    {
        current = current->next;
    }

    bool success = current != nullptr;

    if ( success )
    {
        if ( current->next != nullptr )
        {
            current->next->prev = current->prev;
        }
        // If the class has a pointer to the tail node
        //   then uncomment the else part  
        /*
        else
        {
            tail = current->prev;
        }
        */

        if ( current->prev != nullptr )
        {
            current->prev->next = current->next;
        }
        else
        {
            head = current->next;
        }

        delete current;
    }

    return success;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.