list.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4. typedef struct ListNode {
  5. void* data;
  6. struct ListNode* next;
  7. } ListNode;
  8. ListNode* list_init_head(void* data);
  9. ListNode* list_add(
  10. ListNode* head,
  11. void* data); /* adds element with specified data to the end of the list and returns new head node. */
  12. ListNode* list_find(
  13. ListNode* head,
  14. const void* data); /* returns pointer of element with specified data in list. */
  15. ListNode* list_element_at(
  16. ListNode* head,
  17. uint16_t index); /* returns pointer of element with specified index in list. */
  18. ListNode* list_remove(
  19. ListNode* head,
  20. ListNode* ep); /* removes element from the list and returns new head node. */
  21. void list_free(ListNode* head); /* deletes all elements of the list. */
  22. #define TOTP_LIST_INIT_OR_ADD(head, item, assert) \
  23. do { \
  24. if(head == NULL) { \
  25. head = list_init_head(item); \
  26. assert(head != NULL); \
  27. } else { \
  28. assert(list_add(head, item) != NULL); \
  29. } \
  30. } while(false)