hi again

i'm working on a doubly-linked list at the moment, just one or two questions:

i looked at the way the doubly linked list is implemented in glib, and it 
seems to me a way to create a list is to not call plist_alloc() explicitly, 
but use plist_append(), and have that do the allocating.  and, so far, it
works.  

now, my question:  should i free() the data items in the list cleanup code, or 
leave that to the part of my program where i allocated the data items? i.e.
in the part where i allocated the data items, iterate through the list and
free() all my data items?  that seems to me the more logical approach. 
(i.e. when *data is not a pointer, but for example a static struct that i added
by calling plist_append(list, &struct_name);

i think this code is working, because i can successfully traverse the list
forwards and backwards.

heres part of the code:


typedef struct _PList   PList;
struct _PList {
   void  *data;
   PList *next;
   PList *prev;
};

and these are the functions to allocate/add items.

PList*   plist_alloc(void)
{
   PList *newitem;

   newitem = (PList *) malloc(sizeof(PList));

   if (newitem) {
      newitem->data = NULL;
      newitem->next = NULL;
      newitem->prev = NULL;
   }

   return newitem;
}

PList*   plist_append(PList *list, ppointer data)
{
   PList *new_item, *last_item;

   new_item = plist_alloc();
   new_item->data = data;

   if (list) {
      last_item = plist_last(list);
      last_item->next = new_item;
      new_item->prev = last_item;
      return list;
   } else return new_item;
                                       
}

PList*   plist_last(PList *list)
{
   if (list) {
      while (list->next)
         list = list->next;
   }
   return list;
}
                        



-- 
 Leon Breedt  |  Codewarrior  |  Debian 2.0  |  Linux 2.1.127
 PGP key and homepage at http://ejb.www.icon.co.za

 Linux:  Because I want to get there *today*

Reply via email to