Advertisement
tomdodd4598

Untitled

Oct 21st, 2021
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #pragma once
  2.  
  3. #define DECLARE_LIST(TYPE, CAPITAL, LOWERCASE)\
  4. \
  5. struct Item##CAPITAL {\
  6.     TYPE* value;\
  7.     struct Item##CAPITAL* next;\
  8. };\
  9. \
  10. struct Item##CAPITAL* new_item_##LOWERCASE(TYPE* val);\
  11. \
  12. void delete_item_##LOWERCASE(struct Item##CAPITAL* item);\
  13. \
  14. void insert_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val);\
  15. \
  16. void remove_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val);\
  17. \
  18. void remove_all_##LOWERCASE(struct Item##CAPITAL** start);\
  19. \
  20. struct Item##CAPITAL* print_item_next_##LOWERCASE(struct Item##CAPITAL* item);\
  21. \
  22. void print_list_##LOWERCASE(struct Item##CAPITAL* start);\
  23. \
  24. void print_recursive_##LOWERCASE(struct Item##CAPITAL* start);\
  25.  
  26. #define DEFINE_LIST(TYPE, CAPITAL, LOWERCASE)\
  27. \
  28. struct Item##CAPITAL* new_item_##LOWERCASE(TYPE* val) {\
  29.     printf("Creating item: %s\n", item_functions_##LOWERCASE.value_to_string(val));\
  30.     struct Item##CAPITAL* item = malloc(sizeof *item);\
  31.     if (item != NULL) {\
  32.         item->value = val;\
  33.     }\
  34.     return item;\
  35. }\
  36. \
  37. void delete_item_##LOWERCASE(struct Item##CAPITAL* item) {\
  38.     printf("Deleting item: %s\n", item_functions_##LOWERCASE.value_to_string(item->value));\
  39.     if (item->value != NULL) {\
  40.         item_functions_##LOWERCASE.delete_value(item->value);\
  41.     }\
  42.     if (item->next != NULL) {\
  43.         delete_item_##LOWERCASE(item->next);\
  44.     }\
  45.     free(item);\
  46. }\
  47. \
  48. void insert_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val) {\
  49.     struct Item##CAPITAL* item = new_item_##LOWERCASE(val);\
  50.     if (item != NULL) {\
  51.         while (*start != NULL && !item_functions_##LOWERCASE.insert_before(val, (*start)->value)) {\
  52.             start = &(*start)->next;\
  53.         }\
  54.         \
  55.         item->next = *start;\
  56.         *start = item;\
  57.     }\
  58. }\
  59. \
  60. void remove_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val) {\
  61.     while (*start != NULL && !item_functions_##LOWERCASE.is_value_equal(val, (*start)->value)) {\
  62.         start = &(*start)->next;\
  63.     }\
  64.     \
  65.     if (*start == NULL) {\
  66.         printf("Item %s does not exist!\n", item_functions_##LOWERCASE.value_to_string(val));\
  67.     }\
  68.     else {\
  69.         struct Item##CAPITAL* removed = *start;\
  70.         *start = removed->next;\
  71.         removed->next = NULL;\
  72.         delete_item_##LOWERCASE(removed);\
  73.     }\
  74.     item_functions_##LOWERCASE.delete_value(val);\
  75. }\
  76. \
  77. void remove_all_##LOWERCASE(struct Item##CAPITAL** start) {\
  78.     if (*start != NULL) {\
  79.         delete_item_##LOWERCASE(*start);\
  80.     }\
  81.     *start = NULL;\
  82. }\
  83. \
  84. struct Item##CAPITAL* print_item_next_##LOWERCASE(struct Item##CAPITAL* item) {\
  85.     printf("%s", item_functions_##LOWERCASE.value_to_string(item->value));\
  86.     item = item->next;\
  87.     printf(item == NULL ? "\n" : ", ");\
  88.     return item;\
  89. }\
  90. \
  91. void print_list_##LOWERCASE(struct Item##CAPITAL* start) {\
  92.     while (start != NULL) {\
  93.         start = print_item_next_##LOWERCASE(start);\
  94.     }\
  95. }\
  96. \
  97. void print_recursive_##LOWERCASE(struct Item##CAPITAL* start) {\
  98.     if (start != NULL) {\
  99.         print_recursive_##LOWERCASE(print_item_next_##LOWERCASE(start));\
  100.     }\
  101. }\
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement