src/linked_list.c

changeset 428
da66264af8ad
parent 423
4cea6e50175b
child 435
0fe204d50f54
equal deleted inserted replaced
427:ec92b4ed23aa 428:da66264af8ad
53 53
54 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { 54 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
55 void *last = cx_linked_list_last(begin, end, loc_next); 55 void *last = cx_linked_list_last(begin, end, loc_next);
56 if (last == NULL) { 56 if (last == NULL) {
57 if (begin == NULL) { 57 if (begin == NULL) {
58 // no current list and no begin ptr to write to - we don't find something to append to
58 return 1; 59 return 1;
59 } else { 60 } else {
61 // start fresh list
60 *begin = new_node; 62 *begin = new_node;
61 return 0;
62 } 63 }
63 } 64 } else {
64 65 // if there is a last node, update its next pointer
65 void **next = CX_LL_PTR(last, loc_next); 66 void **next = CX_LL_PTR(last, loc_next);
66 *next = new_node; 67 *next = new_node;
68 }
69
70 // if there is an end pointer, update it
67 if (end != NULL) { 71 if (end != NULL) {
68 *end = cx_linked_list_last(&new_node, NULL, loc_next); 72 *end = cx_linked_list_last(&new_node, NULL, loc_next);
69 } 73 }
74
75 // if the nodes use a prev pointer, update it
70 if (loc_prev >= 0) { 76 if (loc_prev >= 0) {
71 void **prev = CX_LL_PTR(new_node, loc_prev); 77 void **prev = CX_LL_PTR(new_node, loc_prev);
72 *prev = last; 78 *prev = last;
73 } 79 }
74 80

mercurial