Hello,
I am trying the delete all the elements in a linked list in the beginning.
I used the following code, it seems like that it complied fine with ./a.out,
however, when I use valgrind ./a.out, it says there are memory errors.
Could you help me to fix the problem please?
Thank you!
void List::emptyTheList()
{
cout << "inside function emptyTheList() ";
cout <<"starting to traverse list to delete nodes"<<endl;
if (head==NULL)
{
cout<<"there is no elements in the list" <<endl;
}
else
{
DR *temp1;//DR is a class
temp1=head->getNext();
while(temp1!=NULL)
{
free(head);
head=temp1;
temp1=head->getNext();
}
}
} //END function emptyTheList()