Should this code delete a singley linked list?

Started by
11 comments, last by iMalc 19 years, 7 months ago
here's some basic code that i use. You can try to adapt for your's...

//______________________________________________________________________________________void S_LIST::sl_clear() {    struct ListItem *cur,*next;    if ( MyList == 0) return;    for(cur = MyList; cur != 0; cur = next ) {        next = cur->next;        delete (cur);    }    MyList = 0;    return;}
Advertisement
I once wrote a recursively deleting list. Just make things simpler, use iteration. You wouldn't want your list deletion to cause a stack overflow, would you? ;)
Quote:Original post by Tonic151
here's some basic code that i use. You can try to adapt for your's...

*** Source Snippet Removed ***
Not bad, except this line:
    if ( MyList == 0) return;
is completely redundant!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement