Re: Question concerning linked lists

From: Peter Ajamian (pajamian@cheapsam.com)
Date: 12/15/99


You are checking for the NULL value at the end of the list to break out
of your loop if a match isn't found...

>         for(k = spec_door_list; k ; k = k->next) {
>           if( k->door == EXITN(ch->in_room, door)) break;
>         }

...but if that happens you ned up trying to remove that null value...

>         REMOVE_FROM_LIST(k, spec_door_list, next);
>
> now just so you'll know, here's the definition of the macro REMOVE_FROM_LIST
> #define REMOVE_FROM_LIST(item, head, next)  \
if you have no items in the list this would be called with both item and
head being NULL...
>    if ((item) == (head))        \
>       head = (item)->next;      \
Translates to...
if (NULL == NULL) /* true */
  NULL = NULL->next; /* Error! Seg. Fault! */
>    else {               \
>       temp = head;          \
>       while (temp && (temp->next != (item))) \
>      temp = temp->next;     \
>       if (temp)             \
>          temp->next = (item)->next; \
>    }                    \
>
> as a last question:
>   when removing a item from this linked list, i assume i should free it.  I
> have written a free_spec_door() function, but could someone
> more knowledgeable about this give me what is -correct- for a free_spec_door
> function, givin the above structure for spec_door?
> I really appreaciate all the help, and the CM mailing list is already in the
> credits on my login page.  :-)
> thanks all

Try this...

for (k = spec_door_list, j = 0; k; j = k, k = k->next) {
  if (k->door == EXITN(ch->in_room, door)) {
    if (j)
      j->next = k->next; /* k is not the first item in the list so we
                            have to update the pointer of the previous
item */
    else
      spec_door_list = k->next; /* k is the first item in the list so
update
                                   the start of the list instead */

    free(k); /* This is how you free the memory, simple enough ;) */

    break;
  }
}


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



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