Re: [NEWBIE] Chopping off linked lists

From: John Evans (evansj@HI-LINE.NET)
Date: 04/20/98


On Mon, 20 Apr 1998, Mark Gerritsen wrote:

>   for (j = list; j; j = j->next) {
>     if (j->field == 0)
>       REMOVE_FROM_LIST(j, list, next);
>   }

Gotta use a temp variable. Like so:

for (j = list; j; j = next_in_list) {
  next_in_list = j->next;
  if (j->field == 0) {
    REMOVE_FROM_LIST(j, list, next);
  }
}

That way when j gets trashed from the list, the loop will remain intact
because the next_in_list variable carries the next item in the list. You
need to define next_in_list the same way you did j.


John Evans <evansj@hi-line.net>  --  http://www.hi-line.net/~evansj/

Any sufficiently advanced technology is indistinguishable from magic.
  -- Arthur C. Clarke


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST