home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  2.7 KB  |  133 lines

  1. /*    A small List class                          HTList.c
  2. **    ==================
  3. **
  4. **    A list is represented as a sequence of linked nodes of type HTList.
  5. **    The first node is a header which contains no object.
  6. **    New nodes are inserted between the header and the rest of the list.
  7. */
  8.  
  9. #include "HTUtils.h"
  10. #include "HTList.h"
  11.  
  12. /*#include <stdio.h> included by HTUtils.h -- FM *//* joe@athena, TBL 921019 */
  13.  
  14. #include "LYLeaks.h"
  15.  
  16. HTList * HTList_new NOARGS
  17. {
  18.   HTList *newList = (HTList *)calloc(sizeof(HTList), 1);
  19.   if (newList == NULL) outofmem(__FILE__, "HTList_new");
  20.   newList->object = NULL;
  21.   newList->next = NULL;
  22.   return newList;
  23. }
  24.  
  25. void HTList_delete ARGS1(HTList *,me)
  26. {
  27.   HTList *current;
  28.   while (current = me) {
  29.     me = me->next;
  30.     free (current);
  31.   }
  32. }
  33.  
  34. void HTList_addObject ARGS2(HTList *,me, void *,newObject)
  35. {
  36.   if (me) {
  37.     HTList *newNode = (HTList *)calloc(sizeof(HTList), 1);
  38.     if (newNode == NULL) outofmem(__FILE__, "HTList_addObject");
  39.     newNode->object = newObject;
  40.     newNode->next = me->next;
  41.     me->next = newNode;
  42.   }
  43.   else
  44.     if (TRACE) fprintf(stderr,
  45.         "HTList: Trying to add object %p to a nonexisting list\n",
  46.                newObject);
  47. }
  48.  
  49. BOOL HTList_removeObject ARGS2(HTList *,me, void *,oldObject)
  50. {
  51.   if (me) {
  52.     HTList *previous;
  53.     while (me->next) {
  54.       previous = me;
  55.       me = me->next;
  56.       if (me && me->object == oldObject) {
  57.     previous->next = me->next;
  58.     free (me);
  59.     return YES;  /* Success */
  60.       }
  61.     }
  62.   }
  63.   return NO;  /* object not found or NULL list */
  64. }
  65.  
  66. void * HTList_removeLastObject ARGS1 (HTList *,me)
  67. {
  68.   if (me && me->next) {
  69.     HTList *lastNode = me->next;
  70.     void * lastObject = lastNode->object;
  71.     me->next = lastNode->next;
  72.     free (lastNode);
  73.     return lastObject;
  74.   } else  /* Empty list */
  75.     return NULL;
  76. }
  77.  
  78. void * HTList_removeFirstObject ARGS1 (HTList *,me)
  79. {
  80.   if(!me)
  81.     return NULL;
  82.  
  83.   if (me->next) {
  84.     HTList * prevNode;
  85.     void *firstObject;
  86.     while (me->next) {
  87.       prevNode = me;
  88.       me = me->next;
  89.     }
  90.     firstObject = me->object;
  91.     prevNode->next = NULL;
  92.     free (me);
  93.     return firstObject;
  94.   } else  /* Empty list */
  95.     return NULL;
  96. }
  97.  
  98. int HTList_count ARGS1 (HTList *,me)
  99. {
  100.   int count = 0;
  101.   if (me)
  102.     while (me = me->next)
  103.       count++;
  104.   return count;
  105. }
  106.  
  107. int HTList_indexOf ARGS2(HTList *,me, void *,object)
  108. {
  109.   if (me) {
  110.     int position = 0;
  111.     while (me = me->next) {
  112.       if (me->object == object)
  113.     return position;
  114.       position++;
  115.     }
  116.   }
  117.   return -1;  /* Object not in the list */
  118. }
  119.  
  120. void * HTList_objectAt ARGS2 (HTList *,me, int,position)
  121. {
  122.   if (position < 0)
  123.     return NULL;
  124.   if (me) {
  125.     while (me = me->next) {
  126.       if (position == 0)
  127.     return me->object;
  128.       position--;
  129.     }
  130.   }
  131.   return NULL;  /* Reached the end of the list */
  132. }
  133.