HTList.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:3k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*        HTList.c
  2. ** MANAGEMENT OF LINKED LISTS
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** A list is represented as a sequence of linked nodes of type HTList.
  8. ** The first node is a header which contains no object.
  9. ** New nodes are inserted between the header and the rest of the list.
  10. */
  11. /* Library include files */
  12. #include "tcp.h"
  13. #include "HTUtils.h"
  14. #include "HTList.h"
  15. PUBLIC HTList * HTList_new (void)
  16. {
  17.     HTList *newList;
  18.     if ((newList = (HTList  *) HT_CALLOC(1, sizeof (HTList))) == NULL)
  19.         HT_OUTOFMEM("HTList_new");
  20.     newList->object = NULL;
  21.     newList->next = NULL;
  22.     return newList;
  23. }
  24. PUBLIC BOOL HTList_delete (HTList * me)
  25. {
  26.     if (me) {
  27. HTList *current;
  28. while ((current = me)) {
  29.     me = me->next;
  30.     HT_FREE(current);
  31. }
  32. return YES;
  33.     }
  34.     return NO;
  35. }
  36. PUBLIC BOOL HTList_addObject (HTList * me, void * newObject)
  37. {
  38.     if (me) {
  39. HTList *newNode;
  40. if ((newNode = (HTList  *) HT_CALLOC(1, sizeof(HTList))) == NULL)
  41.     HT_OUTOFMEM("HTList_addObject");
  42. newNode->object = newObject;
  43. newNode->next = me->next;
  44. me->next = newNode;
  45. return YES;
  46.     } else {
  47. if (WWWTRACE)
  48.     TTYPrint(TDEST,
  49.     "HTList...... Can not add object %p to nonexisting listn",
  50.     newObject);
  51.     }
  52.     return NO;
  53. }
  54. PUBLIC BOOL HTList_appendObject (HTList * me, void * newObject)
  55. {
  56.     if (me) {
  57. while (me->next) me = me->next;
  58. return HTList_addObject(me, newObject);
  59.     }
  60.     return NO;
  61. }
  62. PUBLIC BOOL HTList_removeObject (HTList *  me, void *  oldObject)
  63. {
  64.     if (me) {
  65. HTList *previous;
  66. while (me->next) {
  67.     previous = me;
  68.     me = me->next;
  69.     if (me->object == oldObject) {
  70. previous->next = me->next;
  71. HT_FREE(me);
  72. return YES; /* Success */
  73.     }
  74. }
  75.     }
  76.     return NO; /* object not found or NULL list */
  77. }
  78. PUBLIC void * HTList_removeLastObject  (HTList * me)
  79. {
  80.     if (me && me->next) {
  81. HTList *lastNode = me->next;
  82. void * lastObject = lastNode->object;
  83. me->next = lastNode->next;
  84. HT_FREE(lastNode);
  85. return lastObject;
  86.     } else /* Empty list */
  87. return NULL;
  88. }
  89. PUBLIC void * HTList_removeFirstObject  (HTList * me)
  90. {
  91.     if (me && me->next) {
  92. HTList * prevNode;
  93. void *firstObject;
  94. while (me->next) {
  95.     prevNode = me;
  96.     me = me->next;
  97. }
  98. firstObject = me->object;
  99. prevNode->next = NULL;
  100. HT_FREE(me);
  101. return firstObject;
  102.     } else /* Empty list */
  103. return NULL;
  104. }
  105. PUBLIC int HTList_count  (HTList * me)
  106. {
  107.     int count = 0;
  108.     if (me)
  109. while ((me = me->next))
  110.     count++;
  111.     return count;
  112. }
  113. PUBLIC int HTList_indexOf (HTList * me, void * object)
  114. {
  115.     if (me) {
  116. int position = 0;
  117. while ((me = me->next)) {
  118.     if (me->object == object)
  119. return position;
  120.     position++;
  121. }
  122.     }
  123.     return -1;
  124. }
  125. PUBLIC void * HTList_objectAt  (HTList * me, int position)
  126. {
  127.     if (position < 0)
  128. return NULL;
  129.     if (me) {
  130. while ((me = me->next)) {
  131.     if (position == 0)
  132. return me->object;
  133.     position--;
  134. }
  135.     }
  136.     return NULL; /* Reached the end of the list */
  137. }
  138. PUBLIC void * HTList_removeObjectAt  (HTList * me, int position)
  139. {
  140.     if (position < 0)
  141. return NULL;
  142.     if (me) {
  143. HTList * prevNode;
  144. prevNode = me;
  145. while ((me = me->next)) {
  146.     if (position == 0) {
  147. prevNode->next = me->next;
  148. return me->object;
  149.     }
  150.     prevNode = me;
  151.     position--;
  152. }
  153.     }
  154.     return NULL;  /* Reached the end of the list */
  155. }