list.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "list.h"
  4. /* Creates a list (node) and returns it
  5. * Arguments: The data the list will contain or NULL to create an empty
  6. * list/node
  7. */
  8. list_node* list_create(void *data)
  9. {
  10. list_node *l = malloc(sizeof(list_node));
  11. if (l != NULL) {
  12. l->next = NULL;
  13. l->data = data;
  14. }
  15. return l;
  16. }
  17. /* Completely destroys a list
  18. * Arguments: A pointer to a pointer to a list
  19. */
  20. void list_destroy(list_node **list)
  21. {
  22. if (list == NULL) return;
  23. while (*list != NULL) {
  24. list_remove(list, *list);
  25. }
  26. }
  27. /* Creates a list node and inserts it after the specified node
  28. * Arguments: A node to insert after and the data the new node will contain
  29. */
  30. list_node* list_insert_after(list_node *node, void *data)
  31. {
  32. list_node *new_node = list_create(data);
  33. if (new_node) {
  34. new_node->next = node->next;
  35. node->next = new_node;
  36. }
  37. return new_node;
  38. }
  39. /* Creates a new list node and inserts it in the beginning of the list
  40. * Arguments: The list the node will be inserted to and the data the node will
  41. * contain
  42. */
  43. list_node* list_insert_beginning(list_node *list, void *data)
  44. {
  45. list_node *new_node = list_create(data);
  46. if (new_node != NULL) { new_node->next = list; }
  47. return new_node;
  48. }
  49. /* Creates a new list node and inserts it at the end of the list
  50. * Arguments: The list the node will be inserted to and the data the node will
  51. * contain
  52. */
  53. list_node* list_insert_end(list_node *list, void *data)
  54. {
  55. list_node *new_node = list_create(data);
  56. if (new_node != NULL)
  57. {
  58. list_node *it;
  59. for(it = list; it != NULL; it = it->next)
  60. {
  61. if (it->next == NULL)
  62. {
  63. it->next = new_node;
  64. break;
  65. }
  66. }
  67. }
  68. return new_node;
  69. }
  70. /* Removes a node from the list
  71. * Arguments: The list and the node that will be removed
  72. */
  73. void list_remove(list_node **list, list_node *node)
  74. {
  75. list_node *tmp = NULL;
  76. if (list == NULL || *list == NULL || node == NULL) return;
  77. if (*list == node) {
  78. *list = (*list)->next;
  79. free(node);
  80. node = NULL;
  81. } else {
  82. tmp = *list;
  83. while (tmp->next && tmp->next != node) tmp = tmp->next;
  84. if (tmp->next) {
  85. tmp->next = node->next;
  86. free(node);
  87. node = NULL;
  88. }
  89. }
  90. }
  91. /* Removes an element from a list by comparing the data pointers
  92. * Arguments: A pointer to a pointer to a list and the pointer to the data
  93. */
  94. void list_remove_by_data(list_node **list, void *data)
  95. {
  96. if (list == NULL || *list == NULL || data == NULL) return;
  97. list_remove(list, list_find_by_data(*list, data));
  98. }
  99. /* Find an element in a list by the pointer to the element
  100. * Arguments: A pointer to a list and a pointer to the node/element
  101. */
  102. list_node* list_find_node(list_node *list, list_node *node)
  103. {
  104. while (list) {
  105. if (list == node) break;
  106. list = list->next;
  107. }
  108. return list;
  109. }
  110. /* Finds an elemt in a list by the data pointer
  111. * Arguments: A pointer to a list and a pointer to the data
  112. */
  113. list_node* list_find_by_data(list_node *list, void *data)
  114. {
  115. while (list) {
  116. if (list->data == data) break;
  117. list = list->next;
  118. }
  119. return list;
  120. }
  121. /* Finds an element in the list by using the comparison function
  122. * Arguments: A pointer to a list, the comparison function and a pointer to the
  123. * data
  124. */
  125. list_node* list_find(list_node *list, int(*func)(list_node*,void*), void *data)
  126. {
  127. if (!func) return NULL;
  128. while(list) {
  129. if (func(list, data)) break;
  130. list = list->next;
  131. }
  132. return list;
  133. }