|
1 | | -#include "list.h" |
2 | | - |
3 | | -struct Head { |
4 | | - Node* front; |
5 | | -}; |
6 | | - |
7 | | -static void iterate_list(Head* head, node_func node_fptr, list_func user_fptr, void* user_func_args) |
8 | | -{ |
9 | | - if(!node_fptr) { |
10 | | - printf("\nNeed to provide function for node iteration\n"); |
11 | | - node_fptr = get_next_node; |
12 | | - } |
13 | | - |
14 | | - if(head) { |
15 | | - Node* front = head->front; |
16 | | - while(front) { |
17 | | - void* node_data = get_node_data(front); |
18 | | - if(user_fptr) { |
19 | | - user_fptr(user_func_args, node_data); |
20 | | - } |
21 | | - front = node_fptr(front); |
22 | | - } |
23 | | - } |
24 | | -} |
25 | | - |
26 | | -static void increment(void* args, void* Node_data) |
27 | | -{ |
28 | | - if(!args) { |
29 | | - printf("\nPass unsigned type output storage\n"); |
30 | | - } |
31 | | - if(!Node_data) |
32 | | - { |
33 | | - printf("\nNode data does not exist\n"); |
34 | | - } |
35 | | - unsigned *count = (unsigned *)args; |
36 | | - if(count) { |
37 | | - ++(*count); |
38 | | - } |
39 | | -} |
40 | | - |
41 | | -Head* create_list(Config* config) |
42 | | -{ |
43 | | - set_config(config); |
44 | | - Head* head = calloc(1, sizeof(Head)); |
45 | | - if(head) return head; |
46 | | - return NULL; |
47 | | -} |
48 | | - |
49 | | -void destroy_list(Head* head, void(* destroy_user_data_func)(void*, void*), void* user_args) |
50 | | -{ |
51 | | - iterate_list(head, destroy_node, destroy_user_data_func, user_args); |
52 | | - free(head); |
53 | | -} |
54 | | - |
55 | | -unsigned count_nodes(Head* head) |
56 | | -{ |
57 | | - unsigned count = 0; |
58 | | - iterate_list(head, get_next_node, increment, &count); |
59 | | - return count; |
60 | | -} |
61 | | - |
62 | | -SLL_Result append_to_list(Head* head, unsigned size, void* data) |
63 | | -{ |
64 | | - if (!head) |
65 | | - { |
66 | | - return SLL_FAIL; |
67 | | - } |
68 | | - |
69 | | - if (!(head->front)) |
70 | | - { |
71 | | - head->front = create_node(); |
72 | | - return fill_node_data(head->front, size, data); |
73 | | - } |
74 | | - else |
75 | | - { |
76 | | - Node* this_node = head->front; |
77 | | - Node* next_node = get_next_node(this_node); |
78 | | - while (next_node) |
79 | | - { |
80 | | - this_node = next_node; |
81 | | - next_node = get_next_node(next_node); |
82 | | - } |
83 | | - return append_node(this_node, size, data); |
84 | | - } |
85 | | -} |
86 | | - |
87 | | -bool find_in_list(Head* head, void* item, bool(* compare_func)(void*, void*)) |
88 | | -{ |
89 | | - if(!compare_func) |
90 | | - { |
91 | | - printf("\nProvide function for data comparison\n"); |
92 | | - return false; |
93 | | - } |
94 | | - if(head) { |
95 | | - Node* front = head->front; |
96 | | - while(front) { |
97 | | - Node* node_data = get_node_data(front); |
98 | | - if(compare_func(item, node_data)) |
99 | | - { |
100 | | - return true; |
101 | | - } |
102 | | - front = get_next_node(front); |
103 | | - } |
104 | | - } |
105 | | - return false; |
106 | | -} |
107 | | - |
108 | | -void set_config(Config* config) |
109 | | -{ |
110 | | - if(config && (config->list_data_alloc_func_fptr || config->list_data_dealloc_func_fptr)) |
111 | | - { |
112 | | - if(NULL == config->list_data_alloc_func_fptr) |
113 | | - { |
114 | | - printf("\nProvide function for data allocation. Aborting.\n"); |
115 | | - exit(EXIT_FAILURE); |
116 | | - } |
117 | | - if(NULL == config->list_data_dealloc_func_fptr) |
118 | | - { |
119 | | - printf("\nProvide function for data deallocation. Aborting.\n"); |
120 | | - exit(EXIT_FAILURE); |
121 | | - } |
122 | | - set_node_data_alloc_dealloc_func(config->list_data_alloc_func_fptr, config->list_data_dealloc_func_fptr); |
123 | | - } |
124 | | - else |
125 | | - { |
126 | | - printf("\nProvide functions for data allocation/dealloc. Using calloc() and free() by default\n"); |
127 | | - } |
128 | | -} |
| 1 | +#include "list.h" |
| 2 | + |
| 3 | +struct Head |
| 4 | +{ |
| 5 | + Node* front; |
| 6 | +}; |
| 7 | + |
| 8 | +static void iterate_list(Head* head, node_func node_fptr, list_func user_fptr, void* user_func_args) |
| 9 | +{ |
| 10 | + if (! node_fptr) { |
| 11 | + printf("\nNeed to provide function for node iteration\n"); |
| 12 | + node_fptr = get_next_node; |
| 13 | + } |
| 14 | + |
| 15 | + if (head) { |
| 16 | + Node* front = head->front; |
| 17 | + while (front) { |
| 18 | + void* node_data = get_node_data(front); |
| 19 | + if (user_fptr) { |
| 20 | + user_fptr(user_func_args, node_data); |
| 21 | + } |
| 22 | + front = node_fptr(front); |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +static void increment(void* args, void* Node_data) |
| 28 | +{ |
| 29 | + if (! args) { |
| 30 | + printf("\nPass unsigned type output storage\n"); |
| 31 | + } |
| 32 | + if (! Node_data) { |
| 33 | + printf("\nNode data does not exist\n"); |
| 34 | + } |
| 35 | + unsigned* count = (unsigned*)args; |
| 36 | + if (count) { |
| 37 | + ++(*count); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +Head* create_list(Config* config) |
| 42 | +{ |
| 43 | + set_config(config); |
| 44 | + Head* head = calloc(1, sizeof(Head)); |
| 45 | + if (head) return head; |
| 46 | + return NULL; |
| 47 | +} |
| 48 | + |
| 49 | +void destroy_list(Head* head, void (*destroy_user_data_func)(void*, void*), void* user_args) |
| 50 | +{ |
| 51 | + iterate_list(head, destroy_node, destroy_user_data_func, user_args); |
| 52 | + free(head); |
| 53 | +} |
| 54 | + |
| 55 | +unsigned count_nodes(Head* head) |
| 56 | +{ |
| 57 | + unsigned count = 0; |
| 58 | + iterate_list(head, get_next_node, increment, &count); |
| 59 | + return count; |
| 60 | +} |
| 61 | + |
| 62 | +SLL_Result append_to_list(Head* head, unsigned size, void* data) |
| 63 | +{ |
| 64 | + if (! head) { |
| 65 | + return SLL_FAIL; |
| 66 | + } |
| 67 | + |
| 68 | + if (! (head->front)) { |
| 69 | + head->front = create_node(); |
| 70 | + return fill_node_data(head->front, size, data); |
| 71 | + } else { |
| 72 | + Node* this_node = head->front; |
| 73 | + Node* next_node = get_next_node(this_node); |
| 74 | + while (next_node) { |
| 75 | + this_node = next_node; |
| 76 | + next_node = get_next_node(next_node); |
| 77 | + } |
| 78 | + return append_node(this_node, size, data); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +bool find_in_list(Head* head, void* item, bool (*compare_func)(void*, void*)) |
| 83 | +{ |
| 84 | + if (! compare_func) { |
| 85 | + printf("\nProvide function for data comparison\n"); |
| 86 | + return false; |
| 87 | + } |
| 88 | + if (head) { |
| 89 | + Node* front = head->front; |
| 90 | + while (front) { |
| 91 | + Node* node_data = get_node_data(front); |
| 92 | + if (compare_func(item, node_data)) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + front = get_next_node(front); |
| 96 | + } |
| 97 | + } |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | +void set_config(Config* config) |
| 102 | +{ |
| 103 | + if (config && (config->list_data_alloc_func_fptr || config->list_data_dealloc_func_fptr)) { |
| 104 | + if (NULL == config->list_data_alloc_func_fptr) { |
| 105 | + printf("\nProvide function for data allocation. Aborting.\n"); |
| 106 | + exit(EXIT_FAILURE); |
| 107 | + } |
| 108 | + if (NULL == config->list_data_dealloc_func_fptr) { |
| 109 | + printf("\nProvide function for data deallocation. Aborting.\n"); |
| 110 | + exit(EXIT_FAILURE); |
| 111 | + } |
| 112 | + set_node_data_alloc_dealloc_func(config->list_data_alloc_func_fptr, config->list_data_dealloc_func_fptr); |
| 113 | + } else { |
| 114 | + printf("\nProvide functions for data allocation/dealloc. Using calloc() and free() by default\n"); |
| 115 | + } |
| 116 | +} |
0 commit comments