I can't think of something much shorter without entering into the ill-advised land of recursion, and even then it would be a stretch.
This will do what you want.
template<typename T>
void LinkedList1<T>::deleteNode(const T& searchValue)
{
discList **pp = &head;
while (*pp && (*pp)->value != searchValue)
pp = &(*pp)->next;
if (*pp)
{
discList *victim = *pp;
*pp = victim->next;
delete victim;
}
}
And before you ask, yes, it works with an empty list and a null head pointer. This algorithm uses the actual pointers in the actual list, not just their values, the pointers, to traverse and destroy. It assumes your list is also terminated with NULL..
Finally, your algorithm (and this one) relies on there being an logical-equality operator defined for CD
, of which I see none. If you've not implemented one yet, you need to do so.