59 |
60 |
60 return last; |
61 return last; |
61 } |
62 } |
62 } |
63 } |
63 |
64 |
64 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { |
65 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { |
65 void *last = cx_linked_list_last(begin, end, loc_next); |
66 void *last = cx_linked_list_last(begin, end, loc_next); |
66 if (last == NULL) { |
67 if (last == NULL) { |
67 if (begin == NULL) { |
68 assert(begin != NULL); |
68 // no current list and no begin ptr to write to - we don't find something to append to |
69 *begin = new_node; |
69 return 1; |
|
70 } else { |
|
71 // start fresh list |
|
72 *begin = new_node; |
|
73 } |
|
74 } else { |
70 } else { |
75 // if there is a last node, update its next pointer |
71 // if there is a last node, update its next pointer |
76 void **next = CX_LL_PTR(last, loc_next); |
72 void **next = CX_LL_PTR(last, loc_next); |
77 *next = new_node; |
73 *next = new_node; |
78 } |
74 } |
85 // if the nodes use a prev pointer, update it |
81 // if the nodes use a prev pointer, update it |
86 if (loc_prev >= 0) { |
82 if (loc_prev >= 0) { |
87 void **prev = CX_LL_PTR(new_node, loc_prev); |
83 void **prev = CX_LL_PTR(new_node, loc_prev); |
88 *prev = last; |
84 *prev = last; |
89 } |
85 } |
90 |
|
91 return 0; |
|
92 } |
86 } |
93 |
87 |
94 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
88 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
95 |
89 |
96 typedef struct cx_linked_list_node cx_linked_list_node; |
90 typedef struct cx_linked_list_node cx_linked_list_node; |