Implement the removeValue function of the LinkedList class
Implement the removeValue function of the LinkedList class .
bool LinkedList::removeValue(int d)
The job of this function is to remove d from the list. There are two cases to consider: either d occurs in the linked list or it doesn\'t. If d does occur, then the first node containing d should be removed. Also, length should be decremented and the value true should be returned. If d doesn\'t occur in the list, then the function will simply return false and leave the list unaltered.
Hint: First check if the list is empty and return false in that case. If the list is nonempty, check if the head node contains d. If it does then just call removeFirst(). If the head node doesn\'t contain d then call findPre(d) and save the result in a variable called pre. If pre->next is null then just return false . If pre->next isn\'t null then call removeAfter(pre) to remove the node. Also return true in this case.
Solution
HI, Please find my implementation.
Please let me know in case of any issue.
bool LinkedList::removeValue(int d){
if(headPtr == NULL)
return false;
if(headPtr->data == d){
removeFirst();
return true;
}
Node *prev = findPre(d);
if(prev == NULL || prev->next == NULL)
return false;
else{
removeAfter(pre);
return true;
}
}
