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;
}
}
curr = nullptr;followed bydelete curr;doesn't make a bunch of sense. Alsocurr = curr->next;immediately after eithercurr = nullptr;ordelete curr;is a bad idea.