linked_list.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4. /**
  5. * @brief Single linked list node
  6. */
  7. typedef struct ListNode {
  8. /**
  9. * @brief Pointer to the data assigned to the current list node
  10. */
  11. void* data;
  12. /**
  13. * @brief Pointer to the next list node
  14. */
  15. struct ListNode* next;
  16. } ListNode;
  17. /**
  18. * @brief Initializes a new list node head
  19. * @param data data to be assigned to the head list node
  20. * @return Head list node
  21. */
  22. ListNode* list_init_head(void* data);
  23. /**
  24. * @brief Adds new list node to the end of the list
  25. * @param head head list node
  26. * @param data data to be assigned to the newly added list node
  27. * @return Head list node
  28. */
  29. ListNode* list_add(
  30. ListNode* head,
  31. void* data); /* adds element with specified data to the end of the list and returns new head node. */
  32. /**
  33. * @brief Searches list node with the given assigned \p data in the list
  34. * @param head head list node
  35. * @param data data to be searched
  36. * @return List node containing \p data if there is such a node in the list; \c NULL otherwise
  37. */
  38. ListNode* list_find(ListNode* head, const void* data);
  39. /**
  40. * @brief Searches list node with the given \p index in the list
  41. * @param head head list node
  42. * @param index desired list node index
  43. * @return List node with the given \p index in the list if there is such a list node; \c NULL otherwise
  44. */
  45. ListNode* list_element_at(ListNode* head, uint16_t index);
  46. /**
  47. * @brief Removes list node from the list
  48. * @param head head list node
  49. * @param ep list node to be removed
  50. * @return Head list node
  51. */
  52. ListNode* list_remove(ListNode* head, ListNode* ep);
  53. /**
  54. * @brief Removes list node with the given \p index in the list from the list
  55. * @param head head list node
  56. * @param index index of the node to be removed
  57. * @param[out] removed_node_data data which was assigned to the removed list node
  58. * @return Head list node
  59. */
  60. ListNode* list_remove_at(ListNode* head, uint16_t index, void** removed_node_data);
  61. /**
  62. * @brief Inserts new list node at the given index
  63. * @param head head list node
  64. * @param index index in the list where the new list node should be inserted
  65. * @param data data to be assgned to the new list node
  66. * @return Head list node
  67. */
  68. ListNode* list_insert_at(ListNode* head, uint16_t index, void* data);
  69. /**
  70. * @brief Disposes all the list nodes in the list
  71. * @param head head list node
  72. */
  73. void list_free(ListNode* head);
  74. #define TOTP_LIST_INIT_OR_ADD(head, item, assert) \
  75. if(head == NULL) { \
  76. head = list_init_head(item); \
  77. assert(head != NULL); \
  78. } else { \
  79. assert(list_add(head, item) != NULL); \
  80. }
  81. #define TOTP_LIST_FOREACH(head, node, action) \
  82. ListNode* node = head; \
  83. while(node != NULL) { \
  84. action node = node->next; \
  85. }