home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / src / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-11  |  1.0 KB  |  59 lines

  1. #include "c.h"
  2.  
  3. static List freenodes;        /* free list nodes */
  4.  
  5. /* append - append x to list, return new list */
  6. List append(void *x, List list)
  7. {
  8.     List new;
  9.  
  10.     if ((new = freenodes) != NULL)
  11.         freenodes = freenodes->link;
  12.     else
  13.         NEW(new, PERM);
  14.     if (list) {
  15.         new->link = list->link;
  16.         list->link = new;
  17.     } else
  18.         new->link = new;
  19.     new->x = x;
  20.     return new;
  21. }
  22.  
  23. /* length - # elements in list */
  24. int length(List list)
  25. {
  26.     int n = 0;
  27.  
  28.     if (list) {
  29.         List lp = list;
  30.         do
  31.             n++;
  32.         while ((lp = lp->link) != list);
  33.     }
  34.     return n;
  35. }
  36.  
  37. /* ltov - convert list to an NULL-terminated vector allocated in arena */
  38. void *ltov(List *list, unsigned arena)
  39. {
  40.     int i = 0;
  41.     void **array = newarray(length(*list) + 1, sizeof array[0], arena);
  42.  
  43.     if (*list) {
  44.         List lp = *list;
  45.         do {
  46.             lp = lp->link;
  47.             array[i++] = lp->x;
  48.         } while (lp != *list);
  49. #ifndef PURIFY
  50.         lp = (*list)->link;
  51.         (*list)->link = freenodes;
  52.         freenodes = lp;
  53. #endif
  54.     }
  55.     *list = NULL;
  56.     array[i] = NULL;
  57.     return array;
  58. }
  59.