list.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. list.c -- functions to deal with double linked lists
  3. Copyright (C) 2000-2005 Ivo Timmermans
  4. 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "list.h"
  19. #include "xalloc.h"
  20. /* (De)constructors */
  21. list_t *list_alloc(list_action_t delete) {
  22. list_t *list;
  23. list = xmalloc_and_zero(sizeof(list_t));
  24. list->delete = delete;
  25. return list;
  26. }
  27. void list_free(list_t *list) {
  28. free(list);
  29. }
  30. list_node_t *list_alloc_node(void) {
  31. return xmalloc_and_zero(sizeof(list_node_t));
  32. }
  33. void list_free_node(list_t *list, list_node_t *node) {
  34. if(node->data && list->delete) {
  35. list->delete(node->data);
  36. }
  37. free(node);
  38. }
  39. /* Insertion and deletion */
  40. list_node_t *list_insert_head(list_t *list, void *data) {
  41. list_node_t *node;
  42. node = list_alloc_node();
  43. node->data = data;
  44. node->prev = NULL;
  45. node->next = list->head;
  46. list->head = node;
  47. if(node->next) {
  48. node->next->prev = node;
  49. } else {
  50. list->tail = node;
  51. }
  52. list->count++;
  53. return node;
  54. }
  55. list_node_t *list_insert_tail(list_t *list, void *data) {
  56. list_node_t *node;
  57. node = list_alloc_node();
  58. node->data = data;
  59. node->next = NULL;
  60. node->prev = list->tail;
  61. list->tail = node;
  62. if(node->prev) {
  63. node->prev->next = node;
  64. } else {
  65. list->head = node;
  66. }
  67. list->count++;
  68. return node;
  69. }
  70. void list_unlink_node(list_t *list, list_node_t *node) {
  71. if(node->prev) {
  72. node->prev->next = node->next;
  73. } else {
  74. list->head = node->next;
  75. }
  76. if(node->next) {
  77. node->next->prev = node->prev;
  78. } else {
  79. list->tail = node->prev;
  80. }
  81. list->count--;
  82. }
  83. void list_delete_node(list_t *list, list_node_t *node) {
  84. list_unlink_node(list, node);
  85. list_free_node(list, node);
  86. }
  87. void list_delete_head(list_t *list) {
  88. list_delete_node(list, list->head);
  89. }
  90. void list_delete_tail(list_t *list) {
  91. list_delete_node(list, list->tail);
  92. }
  93. /* Head/tail lookup */
  94. void *list_get_head(list_t *list) {
  95. if(list->head) {
  96. return list->head->data;
  97. } else {
  98. return NULL;
  99. }
  100. }
  101. void *list_get_tail(list_t *list) {
  102. if(list->tail) {
  103. return list->tail->data;
  104. } else {
  105. return NULL;
  106. }
  107. }
  108. /* Fast list deletion */
  109. void list_delete_list(list_t *list) {
  110. list_node_t *node, *next;
  111. for(node = list->head; node; node = next) {
  112. next = node->next;
  113. list_free_node(list, node);
  114. }
  115. list_free(list);
  116. }
  117. /* Traversing */
  118. void list_foreach_node(list_t *list, list_action_node_t action) {
  119. list_node_t *node, *next;
  120. for(node = list->head; node; node = next) {
  121. next = node->next;
  122. action(node);
  123. }
  124. }
  125. void list_foreach(list_t *list, list_action_t action) {
  126. list_node_t *node, *next;
  127. for(node = list->head; node; node = next) {
  128. next = node->next;
  129. if(node->data) {
  130. action(node->data);
  131. }
  132. }
  133. }