list.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /** ------------------------------------------------------------------------
  2. This file contains routines for manipulating generic lists.
  3. Lists are implemented with a "harness". In other words, each
  4. node in the list consists of two pointers, one to the data item
  5. and one to the next node in the list. The head of the list is
  6. the same struct as each node, but the "item" ptr is used to point
  7. to the current member of the list (used by the first_in_list and
  8. next_in_list functions).
  9. Copyright 1994 Hewlett-Packard Co.
  10. Copyright 1996, 1998 The Open Group
  11. Permission to use, copy, modify, distribute, and sell this software and its
  12. documentation for any purpose is hereby granted without fee, provided that
  13. the above copyright notice appear in all copies and that both that
  14. copyright notice and this permission notice appear in supporting
  15. documentation.
  16. The above copyright notice and this permission notice shall be included
  17. in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25. Except as contained in this notice, the name of The Open Group shall
  26. not be used in advertising or otherwise to promote the sale, use or
  27. other dealings in this Software without prior written authorization
  28. from The Open Group.
  29. ----------------------------------------------------------------------- **/
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "list.h"
  33. /** ------------------------------------------------------------------------
  34. Sets the pointers of the specified list to NULL.
  35. --------------------------------------------------------------------- **/
  36. void zero_list(list_ptr lp)
  37. {
  38. lp->next = NULL;
  39. lp->ptr.item = NULL;
  40. }
  41. /** ------------------------------------------------------------------------
  42. Adds item to the list pointed to by lp. Finds the end of the
  43. list, then mallocs a new list node onto the end of the list.
  44. The item pointer in the new node is set to "item" passed in,
  45. and the next pointer in the new node is set to NULL.
  46. Returns 1 if successful, 0 if the malloc failed.
  47. -------------------------------------------------------------------- **/
  48. int add_to_list(list_ptr lp, void *item)
  49. {
  50. while (lp->next) {
  51. lp = lp->next;
  52. }
  53. if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {
  54. return 0;
  55. }
  56. lp->next->ptr.item = item;
  57. lp->next->next = NULL;
  58. return 1;
  59. }
  60. /** ------------------------------------------------------------------------
  61. Creates a new list and sets its pointers to NULL.
  62. Returns a pointer to the new list.
  63. -------------------------------------------------------------------- **/
  64. list_ptr new_list (void)
  65. {
  66. list_ptr lp;
  67. if ((lp = (list_ptr) malloc( sizeof( list_item)))) {
  68. lp->next = NULL;
  69. lp->ptr.item = NULL;
  70. }
  71. return lp;
  72. }
  73. /** ------------------------------------------------------------------------
  74. Creates a new list head, pointing to the same list as the one
  75. passed in. If start_at_curr is TRUE, the new list's first item
  76. is the "current" item (as set by calls to first/next_in_list()).
  77. If start_at_curr is FALSE, the first item in the new list is the
  78. same as the first item in the old list. In either case, the
  79. curr pointer in the new list is the same as in the old list.
  80. Returns a pointer to the new list head.
  81. -------------------------------------------------------------------- **/
  82. list_ptr dup_list_head(list_ptr lp, int start_at_curr)
  83. {
  84. list_ptr new_listp;
  85. if ((new_listp = (list_ptr) malloc( sizeof( list_item))) == NULL) {
  86. return (list_ptr)NULL;
  87. }
  88. new_listp->next = start_at_curr ? lp->ptr.curr : lp->next;
  89. new_listp->ptr.curr = lp->ptr.curr;
  90. return new_listp;
  91. }
  92. /** ------------------------------------------------------------------------
  93. Returns the number of items in the list.
  94. -------------------------------------------------------------------- **/
  95. unsigned int list_length(list_ptr lp)
  96. {
  97. unsigned int count = 0;
  98. while (lp->next) {
  99. count++;
  100. lp = lp->next;
  101. }
  102. return count;
  103. }
  104. /** ------------------------------------------------------------------------
  105. Scans thru list, looking for a node whose ptr.item is equal to
  106. the "item" passed in. "Equal" here means the same address - no
  107. attempt is made to match equivalent values stored in different
  108. locations. If a match is found, that node is deleted from the
  109. list. Storage for the node is freed, but not for the item itself.
  110. Returns a pointer to the item, so the caller can free it if it
  111. so desires. If a match is not found, returns NULL.
  112. -------------------------------------------------------------------- **/
  113. void *delete_from_list(list_ptr lp, void *item)
  114. {
  115. list_ptr new_next;
  116. while (lp->next) {
  117. if (lp->next->ptr.item == item) {
  118. new_next = lp->next->next;
  119. free (lp->next);
  120. lp->next = new_next;
  121. return item;
  122. }
  123. lp = lp->next;
  124. }
  125. return NULL;
  126. }
  127. /** ------------------------------------------------------------------------
  128. Deletes each node in the list *except the head*. This allows
  129. the deletion of lists where the head is not malloced or created
  130. with new_list(). If free_items is true, each item pointed to
  131. from the node is freed, in addition to the node itself.
  132. -------------------------------------------------------------------- **/
  133. void delete_list(list_ptr lp, int free_items)
  134. {
  135. list_ptr del_node;
  136. void *item;
  137. while (lp->next) {
  138. del_node = lp->next;
  139. item = del_node->ptr.item;
  140. lp->next = del_node->next;
  141. free (del_node);
  142. if (free_items) {
  143. free( item);
  144. }
  145. }
  146. }
  147. void delete_list_destroying(list_ptr lp, void destructor(void *item))
  148. {
  149. list_ptr del_node;
  150. void *item;
  151. while (lp->next) {
  152. del_node = lp->next;
  153. item = del_node->ptr.item;
  154. lp->next = del_node->next;
  155. free( del_node);
  156. if (destructor) {
  157. destructor( item);
  158. }
  159. }
  160. }
  161. /** ------------------------------------------------------------------------
  162. Returns a ptr to the first *item* (not list node) in the list.
  163. Sets the list head node's curr ptr to the first node in the list.
  164. Returns NULL if the list is empty.
  165. -------------------------------------------------------------------- **/
  166. void * first_in_list(list_ptr lp)
  167. {
  168. if (! lp) {
  169. return NULL;
  170. }
  171. lp->ptr.curr = lp->next;
  172. return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
  173. }
  174. /** ------------------------------------------------------------------------
  175. Returns a ptr to the next *item* (not list node) in the list.
  176. Sets the list head node's curr ptr to the next node in the list.
  177. first_in_list must have been called prior.
  178. Returns NULL if no next item.
  179. -------------------------------------------------------------------- **/
  180. void * next_in_list(list_ptr lp)
  181. {
  182. if (! lp) {
  183. return NULL;
  184. }
  185. if (lp->ptr.curr) {
  186. lp->ptr.curr = lp->ptr.curr->next;
  187. }
  188. return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;
  189. }
  190. int list_is_empty(list_ptr lp)
  191. {
  192. return (lp == NULL || lp->next == NULL);
  193. }