I was looking for a bool erase function for an ordered linke
I was looking for a bool erase function for an ordered linked list?
bool ListType<T>::erase(const T& it) {
if(head==0)
{
return false;
}
else
{
struct NodeType<T> *u = head->next,*p = head;
if(head->item == it)
{
head = head->next;
return true;
}
while(u!=0&&u->next!=0)
{
if(u->item == it)
{
p->next = u->next;
return true;
}
u = u->next;
p = p->next;
}
if(u->next==0&&u->item==it)
{
p->next = 0;
return true;
}
return false;
}
}
This one does not seem to work
Solution
Hi, Please find my corrected code.
Please let me know in case of any issue.
bool ListType<T>::erase(const T& it) {
if(head==0)
{
return false;
}
else
{
if(head->item == it)
{
head = head->next;
return true;
}
struct NodeType<T> *u = head;
while(u->next!=0)
{
if(u->next->item == it)
{
u->next = u->next->next;
return true;
}
u = u->next;
}
return false;
}
}

