list.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. do { \
  76. if(head == NULL) { \
  77. head = list_init_head(item); \
  78. assert(head != NULL); \
  79. } else { \
  80. assert(list_add(head, item) != NULL); \
  81. } \
  82. } while(false)
  83. #define TOTP_LIST_FOREACH(head, node, action) \
  84. do { \
  85. ListNode* node = head; \
  86. while(node != NULL) { \
  87. action node = node->next; \
  88. } \
  89. } while(false)