Freeing a list is even simpler:
Quzah.Code:void freeList( Node *n ) { if( n ) freeList( n->next ); free( n ); }
Freeing a list is even simpler:
Quzah.Code:void freeList( Node *n ) { if( n ) freeList( n->next ); free( n ); }
Hope is the first step on the road to disappointment.
In reponse to folks' uncertainties about what happens when a program ends--
In any properly written operating system (which is all except that written by Microsoft -- DOS being the only correctly done O/S), whenever an application ends, the Memory Manager disposes of the heap.
It has no other use for it, so since it's just a RAMblock like any other, it's deallocated.
---
With Microsoft, it's a toss up whether they are failing to deallocate because they are stupid, or whether the code is so bad it's just a "leak"....
enjoy.
> if( n ) freeList( n->next );
Ouch - that could create some unnecessary stack problems
Only if you have circular loop. Otherwise I don't see a problem> if( n ) freeList( n->next );
Ouch - that could create some unnecessary stack problems
(unless you have some massive list, over 100000 elements or
something).
Since no variables are declared in this function, it being recursive
is not really much of a strain. You should have barely any over-
head for the function call.
All you're doing is going to the end of the list, then free it, fall
back one, free that, repeat.
Quzah.
Hope is the first step on the road to disappointment.