aboutsummaryrefslogtreecommitdiff
path: root/list.c
diff options
context:
space:
mode:
Diffstat (limited to 'list.c')
-rw-r--r--list.c264
1 files changed, 0 insertions, 264 deletions
diff --git a/list.c b/list.c
deleted file mode 100644
index d18dae4..0000000
--- a/list.c
+++ /dev/null
@@ -1,264 +0,0 @@
1/* simple Implementation of single linked lists */
2/* written and placed in the public domain by Thomas Schmucker */
3
4/* Standard C */
5#include <stdio.h>
6#include <stdlib.h>
7#include <time.h>
8
9/* Project */
10#include "util.h"
11
12/* --8<-- list_type */
13typedef int T;
14
15struct list_item {
16 struct list_item *next;
17 T data;
18};
19/* -->8-- */
20
21/* --8<-- list_add */
22struct list_item *
23list_add(struct list_item *next, T data)
24{
25 struct list_item *new_item;
26
27 new_item = malloc(sizeof *new_item);
28 if ( new_item ) {
29 new_item->data = data;
30 new_item->next = next;
31 }
32 else {
33 ERROR("out of memory");
34 }
35
36 return new_item;
37}
38/* -->8-- */
39
40/* --8<-- list_insert_next */
41void
42list_insert_next(struct list_item *list, T data)
43{
44 struct list_item *new_item;
45
46 new_item = list_add(list->next, data);
47 if ( new_item ) {
48 list->next = new_item;
49 }
50 else {
51 ERROR("out of memory");
52 }
53}
54/* -->8-- */
55
56/* --8<-- list_delete */
57struct list_item *
58list_delete(struct list_item *list, T data)
59{
60 struct list_item *prev = NULL;
61
62 for ( struct list_item *p = list; p; p = p->next ) {
63 if ( p->data == data ) {
64 if ( prev == NULL ) { /* first element in list? */
65 list = p->next;
66 }
67 else {
68 prev->next = p->next;
69 }
70
71 free(p);
72
73 return list;
74 }
75 prev = p;
76 }
77#ifdef LIST_REPORT_ERROR
78 ERROR("data not found");
79#endif
80 return list;
81}
82/* -->8-- */
83
84/* --8<-- list_delete_next */
85void
86list_delete_next(struct list_item *list)
87{
88 if ( list->next ) {
89 struct list_item *temp = list->next;
90
91 list->next = list->next->next;
92
93 free(temp);
94 }
95}
96/* -->8-- */
97
98/* --8<-- list_length */
99size_t
100list_length(struct list_item *list)
101{
102 size_t len = 0;
103
104 for ( ; list; list = list->next ) {
105 ++len;
106 }
107
108 return len;
109}
110/* -->8-- */
111
112/* --8<-- list_copy */
113struct list_item *
114list_copy(struct list_item *list)
115{
116 struct list_item *head = NULL, **p = &head;
117
118 for ( ; list; list = list->next ) {
119 *p = malloc(sizeof **p);
120 if ( *p ) {
121 (*p)->data = list->data; // copy elements
122 p = &(*p)->next;
123 }
124 else {
125 ERROR("out of memory");
126 }
127 }
128 *p = NULL;
129 return head;
130}
131/* -->8-- */
132
133/* --8<-- list_reverse */
134struct list_item *
135list_reverse(struct list_item *list)
136{
137 struct list_item *head = NULL, *next;
138
139 for ( ; list; list = next ) {
140 next = list->next;
141 list->next = head;
142 head = list;
143 }
144 return head;
145}
146/* -->8-- */
147
148/* --8<-- list_apply */
149void
150list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl)
151{
152 for ( ; list; list = list->next ) {
153 visit(list->data, cl);
154 }
155}
156/* -->8-- */
157
158/* --8<-- list_merge */
159struct list_item *
160list_merge(struct list_item *a, struct list_item *b)
161{
162 struct list_item dummy = { .next = NULL };
163 struct list_item *head = &dummy, *c = head;
164
165 while ( a && b )
166 if ( a->data < b->data ) {
167 c->next = a, c = a, a = a->next;
168 }
169 else {
170 c->next = b, c = b, b = b->next;
171 }
172
173 c->next = a ? a : b;
174
175 return head->next;
176}
177/* -->8-- */
178
179/* --8<-- list_sort */
180struct list_item *
181list_sort(struct list_item *c)
182{
183 if ( c == NULL || c->next == NULL )
184 return c;
185
186 struct list_item *a = c,
187 *b = c->next;
188
189 while ( b && b->next ) {
190 c = c->next, b = b->next->next;
191 }
192
193 b = c->next, c->next = NULL;
194
195 return list_merge(list_sort(a), list_sort(b));
196}
197/* -->8-- */
198
199/* --8<-- list_free */
200void
201list_free(struct list_item *list)
202{
203 struct list_item *next;
204
205 for ( ; list; list = next ) {
206 next = list->next;
207 free(list);
208 }
209}
210/* -->8-- */
211
212/* --8<-- list_apply_sample */
213static void
214print_data(T data, void *cl)
215{
216 fprintf(cl, "%d\n", data);
217}
218/* -->8-- */
219
220int
221main()
222{
223 clock_t start;
224
225 /*
226 struct list_item *mylist = NULL;
227
228 mylist = list_add(mylist, 42);
229 mylist = list_add(mylist, 43);
230 mylist = list_add(mylist, 44);
231
232 mylist = list_delete(mylist, 45);
233
234 list_apply(mylist, print_data, stdout);
235
236 struct list_item *mylist2 = list_reverse(list_copy(mylist));
237
238 list_free(mylist);
239 list_apply(mylist2, print_data);
240 list_free(mylist2);
241 */
242
243 srand(time(NULL));
244
245 static const int COUNT = 10000000;
246
247 struct list_item *x = NULL;
248 for ( int i = 0; i != COUNT; ++i ) {
249 int r = rand();
250 x = list_add(x, r);
251 }
252
253 puts("start");
254 start = clock();
255 x = list_sort(x);
256 printf("Fertig: %.3lf sec\n", (double) (clock() - start) / CLOCKS_PER_SEC);
257
258 //list_apply(x, print_data);
259 printf("Len: %zu\n", list_length(x));
260
261 list_free(x);
262
263 return EXIT_SUCCESS;
264}