Hello,
Could someone please post some code for freeing all the nodes of a linked list in the most efficient manner.
The nodes pointer to next have to be referenced to null and then freed. I keep getting errors when I try to implement it.
I know its simple. Could anyone please post the code please?
Thanks,
Mistro116
Edit:
The code I have so far is:
Code:
void DeleteList (NODEPTR headPtr)
{
NODEPTR curr, prev;
if (IsEmpty (headPtr))
{
printf ("\nThe list is empty - All nodes have been ");
printf ("deleted and freed.\n\n");
}
else
{
prev = NULL;
curr = headPtr;
while (prev != NULL)
{
/* Traverse the list. */
prev = curr;
curr = curr -> next;
prev -> next = NULL;
free (prev);
}
/* Insert the node, temp, at the end of the list. */
headPtr = NULL;
}
}