aboutsummaryrefslogtreecommitdiff
path: root/list.c
diff options
context:
space:
mode:
Diffstat (limited to 'list.c')
-rw-r--r--list.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/list.c b/list.c
index da0c702..24024da 100644
--- a/list.c
+++ b/list.c
@@ -9,13 +9,16 @@
9/* Project */ 9/* Project */
10#include "util.h" 10#include "util.h"
11 11
12/* --8<-- list_type */
12typedef int T; 13typedef int T;
13 14
14struct list_item { 15struct list_item {
15 struct list_item *next; 16 struct list_item *next;
16 T data; 17 T data;
17}; 18};
19/* -->8-- */
18 20
21/* --8<-- list_add */
19struct list_item * 22struct list_item *
20list_add(struct list_item *next, T data) 23list_add(struct list_item *next, T data)
21{ 24{
@@ -31,7 +34,9 @@ list_add(struct list_item *next, T data)
31 34
32 return new_item; 35 return new_item;
33} 36}
37/* -->8-- */
34 38
39/* --8<-- list_insert_next */
35void 40void
36list_insert_next(struct list_item *list, T data) 41list_insert_next(struct list_item *list, T data)
37{ 42{
@@ -47,7 +52,9 @@ list_insert_next(struct list_item *list, T data)
47 else 52 else
48 ERROR("out of memory"); 53 ERROR("out of memory");
49} 54}
55/* -->8-- */
50 56
57/* --8<-- list_delete */
51struct list_item * 58struct list_item *
52list_delete(struct list_item *list, T data) 59list_delete(struct list_item *list, T data)
53{ 60{
@@ -68,10 +75,14 @@ list_delete(struct list_item *list, T data)
68 } 75 }
69 prev = p; 76 prev = p;
70 } 77 }
71 //ERROR("data not found"); /* uncomment, if this case should be reported as an error */ 78#if LIST_REPORT_ERROR
79 ERROR("data not found");
80#endif
72 return list; 81 return list;
73} 82}
83/* -->8-- */
74 84
85/* --8<-- list_delete_next */
75void 86void
76list_delete_next(struct list_item *list) 87list_delete_next(struct list_item *list)
77{ 88{
@@ -83,7 +94,9 @@ list_delete_next(struct list_item *list)
83 free(temp); 94 free(temp);
84 } 95 }
85} 96}
97/* -->8-- */
86 98
99/* --8<-- list_length */
87size_t 100size_t
88list_length(struct list_item *list) 101list_length(struct list_item *list)
89{ 102{
@@ -94,7 +107,9 @@ list_length(struct list_item *list)
94 107
95 return len; 108 return len;
96} 109}
110/* -->8-- */
97 111
112/* --8<-- list_copy */
98struct list_item * 113struct list_item *
99list_copy(struct list_item *list) 114list_copy(struct list_item *list)
100{ 115{
@@ -112,7 +127,9 @@ list_copy(struct list_item *list)
112 *p = NULL; 127 *p = NULL;
113 return head; 128 return head;
114} 129}
130/* -->8-- */
115 131
132/* --8<-- list_reverse */
116struct list_item * 133struct list_item *
117list_reverse(struct list_item *list) 134list_reverse(struct list_item *list)
118{ 135{
@@ -125,7 +142,9 @@ list_reverse(struct list_item *list)
125 } 142 }
126 return head; 143 return head;
127} 144}
145/* -->8-- */
128 146
147/* --8<-- list_apply */
129void 148void
130list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl) 149list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl)
131{ 150{
@@ -133,7 +152,9 @@ list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl)
133 visit(list->data, cl); 152 visit(list->data, cl);
134 } 153 }
135} 154}
155/* -->8-- */
136 156
157/* --8<-- list_merge */
137struct list_item * 158struct list_item *
138list_merge(struct list_item *a, struct list_item *b) 159list_merge(struct list_item *a, struct list_item *b)
139{ 160{
@@ -150,7 +171,9 @@ list_merge(struct list_item *a, struct list_item *b)
150 171
151 return head->next; 172 return head->next;
152} 173}
174/* -->8-- */
153 175
176/* --8<-- list_sort */
154struct list_item * 177struct list_item *
155list_sort(struct list_item *c) 178list_sort(struct list_item *c)
156{ 179{
@@ -167,7 +190,9 @@ list_sort(struct list_item *c)
167 190
168 return list_merge(list_sort(a), list_sort(b)); 191 return list_merge(list_sort(a), list_sort(b));
169} 192}
193/* -->8-- */
170 194
195/* --8<-- list_free */
171void 196void
172list_free(struct list_item *list) 197list_free(struct list_item *list)
173{ 198{
@@ -178,12 +203,15 @@ list_free(struct list_item *list)
178 free(list); 203 free(list);
179 } 204 }
180} 205}
206/* -->8-- */
181 207
208/* --8<-- list_apply_sample */
182static void 209static void
183print_data(T data, void *cl) 210print_data(T data, void *cl)
184{ 211{
185 fprintf(cl, "%d\n", data); 212 fprintf(cl, "%d\n", data);
186} 213}
214/* -->8-- */
187 215
188int 216int
189main() 217main()