list.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "list.h"
  2. ListNode* list_init_head(void* data) {
  3. ListNode* new = malloc(sizeof(ListNode));
  4. if(new == NULL) return NULL;
  5. new->data = data;
  6. new->next = NULL;
  7. return new;
  8. }
  9. ListNode* list_add(ListNode* head, void* data) {
  10. ListNode* new = malloc(sizeof(ListNode));
  11. if(new == NULL) return NULL;
  12. new->data = data;
  13. new->next = NULL;
  14. if(head == NULL)
  15. head = new;
  16. else {
  17. ListNode* it;
  18. for(it = head; it->next != NULL; it = it->next)
  19. ;
  20. it->next = new;
  21. }
  22. return head;
  23. }
  24. ListNode* list_find(ListNode* head, const void* data) {
  25. ListNode* it = NULL;
  26. for(it = head; it != NULL; it = it->next)
  27. if(it->data == data) break;
  28. return it;
  29. }
  30. ListNode* list_element_at(ListNode* head, uint16_t index) {
  31. ListNode* it;
  32. uint16_t i;
  33. for(it = head, i = 0; it != NULL && i < index; it = it->next, i++)
  34. ;
  35. return it;
  36. }
  37. ListNode* list_remove(ListNode* head, ListNode* ep) {
  38. if(head == NULL) {
  39. return NULL;
  40. }
  41. if(head == ep) {
  42. ListNode* new_head = head->next;
  43. free(head);
  44. return new_head;
  45. }
  46. ListNode* it;
  47. for(it = head; it->next != ep; it = it->next)
  48. ;
  49. it->next = ep->next;
  50. free(ep);
  51. return head;
  52. }
  53. ListNode* list_remove_at(ListNode* head, uint16_t index, void** removed_node_data) {
  54. if(head == NULL) {
  55. return NULL;
  56. }
  57. ListNode* it;
  58. ListNode* prev = NULL;
  59. uint16_t i;
  60. for(it = head, i = 0; it != NULL && i < index; prev = it, it = it->next, i++)
  61. ;
  62. if(it == NULL) return head;
  63. ListNode* new_head = head;
  64. if(prev == NULL) {
  65. new_head = it->next;
  66. } else {
  67. prev->next = it->next;
  68. }
  69. if(removed_node_data != NULL) {
  70. *removed_node_data = it->data;
  71. }
  72. free(it);
  73. return new_head;
  74. }
  75. ListNode* list_insert_at(ListNode* head, uint16_t index, void* data) {
  76. if(index == 0 || head == NULL) {
  77. ListNode* new_head = list_init_head(data);
  78. if(new_head != NULL) {
  79. new_head->next = head;
  80. }
  81. return new_head;
  82. }
  83. ListNode* it;
  84. ListNode* prev = NULL;
  85. uint16_t i;
  86. for(it = head, i = 0; it != NULL && i < index; prev = it, it = it->next, i++)
  87. ;
  88. ListNode* new = malloc(sizeof(ListNode));
  89. if(new == NULL) return NULL;
  90. new->data = data;
  91. new->next = it;
  92. prev->next = new;
  93. return head;
  94. }
  95. void list_free(ListNode* head) {
  96. ListNode* it = head;
  97. ListNode* tmp;
  98. while(it != NULL) {
  99. tmp = it;
  100. it = it->next;
  101. free(tmp);
  102. }
  103. }