Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #define DECLARE_LIST(TYPE, CAPITAL, LOWERCASE)\
- \
- struct Item##CAPITAL {\
- TYPE* value;\
- struct Item##CAPITAL* next;\
- };\
- \
- struct Item##CAPITAL* new_item_##LOWERCASE(TYPE* val);\
- \
- void delete_item_##LOWERCASE(struct Item##CAPITAL* item);\
- \
- void insert_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val);\
- \
- void remove_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val);\
- \
- void remove_all_##LOWERCASE(struct Item##CAPITAL** start);\
- \
- struct Item##CAPITAL* print_item_next_##LOWERCASE(struct Item##CAPITAL* item);\
- \
- void print_list_##LOWERCASE(struct Item##CAPITAL* start);\
- \
- void print_recursive_##LOWERCASE(struct Item##CAPITAL* start);\
- #define DEFINE_LIST(TYPE, CAPITAL, LOWERCASE)\
- \
- struct Item##CAPITAL* new_item_##LOWERCASE(TYPE* val) {\
- printf("Creating item: %s\n", item_functions_##LOWERCASE.value_to_string(val));\
- struct Item##CAPITAL* item = malloc(sizeof *item);\
- if (item != NULL) {\
- item->value = val;\
- }\
- return item;\
- }\
- \
- void delete_item_##LOWERCASE(struct Item##CAPITAL* item) {\
- printf("Deleting item: %s\n", item_functions_##LOWERCASE.value_to_string(item->value));\
- if (item->value != NULL) {\
- item_functions_##LOWERCASE.delete_value(item->value);\
- }\
- if (item->next != NULL) {\
- delete_item_##LOWERCASE(item->next);\
- }\
- free(item);\
- }\
- \
- void insert_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val) {\
- struct Item##CAPITAL* item = new_item_##LOWERCASE(val);\
- if (item != NULL) {\
- while (*start != NULL && !item_functions_##LOWERCASE.insert_before(val, (*start)->value)) {\
- start = &(*start)->next;\
- }\
- \
- item->next = *start;\
- *start = item;\
- }\
- }\
- \
- void remove_item_##LOWERCASE(struct Item##CAPITAL** start, TYPE* val) {\
- while (*start != NULL && !item_functions_##LOWERCASE.is_value_equal(val, (*start)->value)) {\
- start = &(*start)->next;\
- }\
- \
- if (*start == NULL) {\
- printf("Item %s does not exist!\n", item_functions_##LOWERCASE.value_to_string(val));\
- }\
- else {\
- struct Item##CAPITAL* removed = *start;\
- *start = removed->next;\
- removed->next = NULL;\
- delete_item_##LOWERCASE(removed);\
- }\
- item_functions_##LOWERCASE.delete_value(val);\
- }\
- \
- void remove_all_##LOWERCASE(struct Item##CAPITAL** start) {\
- if (*start != NULL) {\
- delete_item_##LOWERCASE(*start);\
- }\
- *start = NULL;\
- }\
- \
- struct Item##CAPITAL* print_item_next_##LOWERCASE(struct Item##CAPITAL* item) {\
- printf("%s", item_functions_##LOWERCASE.value_to_string(item->value));\
- item = item->next;\
- printf(item == NULL ? "\n" : ", ");\
- return item;\
- }\
- \
- void print_list_##LOWERCASE(struct Item##CAPITAL* start) {\
- while (start != NULL) {\
- start = print_item_next_##LOWERCASE(start);\
- }\
- }\
- \
- void print_recursive_##LOWERCASE(struct Item##CAPITAL* start) {\
- if (start != NULL) {\
- print_recursive_##LOWERCASE(print_item_next_##LOWERCASE(start));\
- }\
- }\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement