diff options
Diffstat (limited to 'list.c')
| -rw-r--r-- | list.c | 203 |
1 files changed, 203 insertions, 0 deletions
| @@ -0,0 +1,203 @@ | |||
| 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 | typedef int T; | ||
| 13 | |||
| 14 | struct list_item { | ||
| 15 | struct list_item *next; | ||
| 16 | T data; | ||
| 17 | }; | ||
| 18 | |||
| 19 | struct list_item * | ||
| 20 | list_add(struct list_item *next, T data) | ||
| 21 | { | ||
| 22 | struct list_item *new_item; | ||
| 23 | |||
| 24 | new_item = malloc(sizeof *new_item); | ||
| 25 | if ( new_item != NULL ) { | ||
| 26 | new_item->next = next; | ||
| 27 | new_item->data = data; | ||
| 28 | } | ||
| 29 | else | ||
| 30 | ERROR("out of memory"); | ||
| 31 | |||
| 32 | return new_item; | ||
| 33 | } | ||
| 34 | |||
| 35 | struct list_item * | ||
| 36 | list_delete(struct list_item *list, T data) | ||
| 37 | { | ||
| 38 | struct list_item *prev = NULL; | ||
| 39 | |||
| 40 | for ( struct list_item *p = list; p; p = p->next ) { | ||
| 41 | if ( p->data == data ) { | ||
| 42 | if ( prev == NULL ) { /* first element in list? */ | ||
| 43 | list = p->next; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | prev->next = p->next; | ||
| 47 | } | ||
| 48 | |||
| 49 | free(p); | ||
| 50 | |||
| 51 | return list; | ||
| 52 | } | ||
| 53 | prev = p; | ||
| 54 | } | ||
| 55 | //ERROR("data not found"); /* uncomment, if this case should be reported as an error */ | ||
| 56 | return list; | ||
| 57 | } | ||
| 58 | |||
| 59 | size_t | ||
| 60 | list_length(struct list_item *list) | ||
| 61 | { | ||
| 62 | size_t len = 0; | ||
| 63 | |||
| 64 | for ( ; list; list = list->next ) | ||
| 65 | ++len; | ||
| 66 | |||
| 67 | return len; | ||
| 68 | } | ||
| 69 | |||
| 70 | struct list_item * | ||
| 71 | list_copy(struct list_item *list) | ||
| 72 | { | ||
| 73 | struct list_item *head, **p = &head; | ||
| 74 | |||
| 75 | for ( ; list; list = list->next ) { | ||
| 76 | *p = malloc(sizeof **p); | ||
| 77 | if ( *p != NULL ) { | ||
| 78 | (*p)->data = list->data; // copy elements | ||
| 79 | p = &(*p)->next; | ||
| 80 | } | ||
| 81 | else | ||
| 82 | ERROR("out of memory"); | ||
| 83 | } | ||
| 84 | *p = NULL; | ||
| 85 | return head; | ||
| 86 | } | ||
| 87 | |||
| 88 | struct list_item * | ||
| 89 | list_reverse(struct list_item *list) | ||
| 90 | { | ||
| 91 | struct list_item *head = NULL, *next; | ||
| 92 | |||
| 93 | for ( ; list; list = next ) { | ||
| 94 | next = list->next; | ||
| 95 | list->next = head; | ||
| 96 | head = list; | ||
| 97 | } | ||
| 98 | return head; | ||
| 99 | } | ||
| 100 | |||
| 101 | void | ||
| 102 | list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl) | ||
| 103 | { | ||
| 104 | for ( ; list; list = list->next ) { | ||
| 105 | visit(list->data, cl); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | struct list_item * | ||
| 110 | list_merge(struct list_item *a, struct list_item *b) | ||
| 111 | { | ||
| 112 | struct list_item dummy = { .next = NULL }; | ||
| 113 | struct list_item *head = &dummy, *c = head; | ||
| 114 | |||
| 115 | while ( a != NULL && b != NULL ) | ||
| 116 | if ( a->data < b->data ) | ||
| 117 | c->next = a, c = a, a = a->next; | ||
| 118 | else | ||
| 119 | c->next = b, c = b, b = b->next; | ||
| 120 | |||
| 121 | c->next = ( a != NULL ) ? a : b; | ||
| 122 | |||
| 123 | return head->next; | ||
| 124 | } | ||
| 125 | |||
| 126 | struct list_item * | ||
| 127 | list_sort(struct list_item *c) | ||
| 128 | { | ||
| 129 | if ( c == NULL || c->next == NULL ) | ||
| 130 | return c; | ||
| 131 | |||
| 132 | struct list_item *a = c, | ||
| 133 | *b = c->next; | ||
| 134 | |||
| 135 | while ( b != NULL && b->next != NULL ) | ||
| 136 | c = c->next, b = b->next->next; | ||
| 137 | |||
| 138 | b = c->next, c->next = NULL; | ||
| 139 | |||
| 140 | return list_merge(list_sort(a), list_sort(b)); | ||
| 141 | } | ||
| 142 | |||
| 143 | void | ||
| 144 | list_free(struct list_item *list) | ||
| 145 | { | ||
| 146 | struct list_item *next; | ||
| 147 | |||
| 148 | for ( ; list; list = next ) { | ||
| 149 | next = list->next; | ||
| 150 | free(list); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | static void print_data(T data, void *cl) { fprintf(cl, "%d\n", data); } | ||
| 155 | |||
| 156 | int | ||
| 157 | main() | ||
| 158 | { | ||
| 159 | clock_t start; | ||
| 160 | |||
| 161 | /* | ||
| 162 | struct list_item *mylist = NULL; | ||
| 163 | |||
| 164 | mylist = list_add(mylist, 42); | ||
| 165 | mylist = list_add(mylist, 43); | ||
| 166 | mylist = list_add(mylist, 44); | ||
| 167 | |||
| 168 | mylist = list_delete(mylist, 45); | ||
| 169 | |||
| 170 | list_apply(mylist, print_data, stdout); | ||
| 171 | |||
| 172 | struct list_item *mylist2 = list_reverse(list_copy(mylist)); | ||
| 173 | |||
| 174 | list_free(mylist); | ||
| 175 | list_apply(mylist2, print_data); | ||
| 176 | list_free(mylist2); | ||
| 177 | */ | ||
| 178 | |||
| 179 | srand(time(NULL)); | ||
| 180 | |||
| 181 | static const int COUNT = 10000000; | ||
| 182 | |||
| 183 | struct list_item *x = NULL; | ||
| 184 | for ( int i = 0; i != COUNT; ++i ) { | ||
| 185 | int r = rand(); | ||
| 186 | x = list_add(x, r); | ||
| 187 | } | ||
| 188 | |||
| 189 | puts("start"); | ||
| 190 | start = clock(); | ||
| 191 | x = list_sort(x); | ||
| 192 | printf("Fertig: %.3lf sec\n", (double)(clock() - start) / CLOCKS_PER_SEC); | ||
| 193 | |||
| 194 | //list_apply(x, print_data); | ||
| 195 | printf("Len: %zu\n", list_length(x)); | ||
| 196 | |||
| 197 | list_free(x); | ||
| 198 | |||
| 199 | return EXIT_SUCCESS; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | |||
