|
1 # linked_list.h |
|
2 |
|
3 On top of implementing the list interface, this header also defines several low-level functions that |
|
4 work with arbitrary structures. |
|
5 Low-level functions, in contrast to the high-level list interface, can easily be recognized by their snake-casing. |
|
6 The function `cx_linked_list_at`, for example, implements a similar functionality like `cxListAt`, but operates |
|
7 on arbitrary structures. |
|
8 The following snippet shows how it is used. |
|
9 All other low-level functions work similarly. |
|
10 ```c |
|
11 struct node { |
|
12 node *next; |
|
13 node *prev; |
|
14 int data; |
|
15 }; |
|
16 |
|
17 const ptrdiff_t loc_prev = offsetof(struct node, prev); |
|
18 const ptrdiff_t loc_next = offsetof(struct node, next); |
|
19 const ptrdiff_t loc_data = offsetof(struct node, data); |
|
20 |
|
21 struct node a = {0}, b = {0}, c = {0}, d = {0}; |
|
22 cx_linked_list_link(&a, &b, loc_prev, loc_next); |
|
23 cx_linked_list_link(&b, &c, loc_prev, loc_next); |
|
24 cx_linked_list_link(&c, &d, loc_prev, loc_next); |
|
25 |
|
26 cx_linked_list_at(&a, 0, loc_next, 2); // returns pointer to c |
|
27 ``` |