Re: [CODE CONCEPTS] Linked lists / reenterant list iterators

From: Gary Barnett (gbarnett@POLARNET.COM)
Date: 03/25/98


From: Chris Jacobson <fear@ATHENET.NET>

>So I was thinking, what would be the SAFEST way to implement linked
>lists, and to iterate through the list without losing place, when there
>is the chance that the current, previous, next, or any other item in the
>list could be removed in mid-iteration....
>
<snip>

How about marking any 'deleted' entry with a flag variable, then using that
variable to skip processing that element of the list.

Then have a seperate routine to run  every so often to compact the list. I
assume you only have one thread and just need to do stuff inside the loop
construct that might affect the linked list in a random location.

That'd keep your links happy and allow simple code (read: easy to debug). I
use this approach in a few places on Antares and find it works quite well.

Something like this fragment :-)

#define my_stuff_flag_deleted  (1 <<0)
#define my_stuff_flag_blah1    (1 <<1)
...
#define my_stuff_flag_blahn    (1 <<n)

struct my_stuff {
    int flags;
    int mynumber;
    char *mystring;
};
struct my_stuff *stuff;

void look_through_stuff () {
   struct my_stuff *stuff_next, *blah;
   for (blah=stuff;blah;blah=stuff_next) {
       stuff_next=blah->next;
       if (IS_SET(blah->flags, my_stuff_flag_deleted))
           continue;
       /* handle element */
   }
}

--Mallory


     +------------------------------------------------------------+
     | 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