diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-02 10:38:41 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-02 10:38:41 +0200 |
| commit | e7986e3e11b9ca91079bacae7fc2f79dce515f5d (patch) | |
| tree | 8445b05e4b1f3f8c0dfeef5f9e6d023f52ad108d | |
| parent | 1aecbf40567f0f1943285c774b4d6369e1a90ceb (diff) | |
| download | data-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.tar.gz data-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.tar.bz2 data-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.zip | |
format code
| -rw-r--r-- | hashtab.c | 51 | ||||
| -rw-r--r-- | list.c | 35 | ||||
| -rw-r--r-- | rb.c | 4 |
3 files changed, 50 insertions, 40 deletions
| @@ -1,8 +1,8 @@ | |||
| 1 | #include <ctype.h> | ||
| 2 | #include <stdbool.h> | ||
| 1 | #include <stdio.h> | 3 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 3 | #include <string.h> | 5 | #include <string.h> |
| 4 | #include <stdbool.h> | ||
| 5 | #include <ctype.h> | ||
| 6 | 6 | ||
| 7 | #include "util.h" | 7 | #include "util.h" |
| 8 | 8 | ||
| @@ -10,19 +10,19 @@ typedef int T; | |||
| 10 | 10 | ||
| 11 | struct hash_item { | 11 | struct hash_item { |
| 12 | struct hash_item *next; | 12 | struct hash_item *next; |
| 13 | char *key; | 13 | char * key; |
| 14 | T data; | 14 | T data; |
| 15 | }; | 15 | }; |
| 16 | 16 | ||
| 17 | struct hash_tab { | 17 | struct hash_tab { |
| 18 | struct hash_item *table[251]; // fit for your needs... | 18 | struct hash_item *table[251]; // fit for your needs... |
| 19 | }; | 19 | }; |
| 20 | 20 | ||
| 21 | static unsigned long | 21 | static unsigned long |
| 22 | hash_key(const unsigned char *str) | 22 | hash_key(const unsigned char *str) |
| 23 | { | 23 | { |
| 24 | unsigned long hash = 5381; | 24 | unsigned long hash = 5381; |
| 25 | int c; | 25 | int c; |
| 26 | 26 | ||
| 27 | while ( (c = *str++) != '\0' ) | 27 | while ( (c = *str++) != '\0' ) |
| 28 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ | 28 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ |
| @@ -41,9 +41,9 @@ static struct hash_item * | |||
| 41 | hash_add(struct hash_item *next, const char *key, T data) | 41 | hash_add(struct hash_item *next, const char *key, T data) |
| 42 | { | 42 | { |
| 43 | struct hash_item *new_item; | 43 | struct hash_item *new_item; |
| 44 | char *new_key; | 44 | char * new_key; |
| 45 | 45 | ||
| 46 | new_key = strdup(key); // strdup: not standard but commonly used... | 46 | new_key = strdup(key); // strdup: not standard but commonly used... |
| 47 | new_item = malloc(sizeof(*new_item)); | 47 | new_item = malloc(sizeof(*new_item)); |
| 48 | 48 | ||
| 49 | if ( new_key == NULL || new_item == NULL ) { | 49 | if ( new_key == NULL || new_item == NULL ) { |
| @@ -54,7 +54,7 @@ hash_add(struct hash_item *next, const char *key, T data) | |||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | new_item->next = next; | 56 | new_item->next = next; |
| 57 | new_item->key = new_key; | 57 | new_item->key = new_key; |
| 58 | new_item->data = data; | 58 | new_item->data = data; |
| 59 | 59 | ||
| 60 | return new_item; | 60 | return new_item; |
| @@ -63,9 +63,9 @@ hash_add(struct hash_item *next, const char *key, T data) | |||
| 63 | T * | 63 | T * |
| 64 | hash_lookup(struct hash_tab *ht, const char *key, T data, int create) | 64 | hash_lookup(struct hash_tab *ht, const char *key, T data, int create) |
| 65 | { | 65 | { |
| 66 | unsigned long h; | 66 | unsigned long h; |
| 67 | struct hash_item *item; | 67 | struct hash_item *item; |
| 68 | 68 | ||
| 69 | h = hash_key((const unsigned char *) key) % NELEM(ht->table); | 69 | h = hash_key((const unsigned char *) key) % NELEM(ht->table); |
| 70 | for ( item = ht->table[h]; item; item = item->next ) | 70 | for ( item = ht->table[h]; item; item = item->next ) |
| 71 | if ( strcmp(key, item->key) == 0 ) | 71 | if ( strcmp(key, item->key) == 0 ) |
| @@ -86,10 +86,10 @@ hash_lookup(struct hash_tab *ht, const char *key, T data, int create) | |||
| 86 | bool | 86 | bool |
| 87 | hash_delete(struct hash_tab *ht, const char *key) | 87 | hash_delete(struct hash_tab *ht, const char *key) |
| 88 | { | 88 | { |
| 89 | unsigned long h; | 89 | unsigned long h; |
| 90 | struct hash_item *prev, *p; | 90 | struct hash_item *prev, *p; |
| 91 | 91 | ||
| 92 | h = hash_key((const unsigned char *) key) % NELEM(ht->table); | 92 | h = hash_key((const unsigned char *) key) % NELEM(ht->table); |
| 93 | prev = NULL; | 93 | prev = NULL; |
| 94 | for ( p = ht->table[h]; p; p = p->next ) { | 94 | for ( p = ht->table[h]; p; p = p->next ) { |
| 95 | if ( strcmp(key, p->key) == 0 ) { | 95 | if ( strcmp(key, p->key) == 0 ) { |
| @@ -101,7 +101,7 @@ hash_delete(struct hash_tab *ht, const char *key) | |||
| 101 | free(p->key); | 101 | free(p->key); |
| 102 | free(p); | 102 | free(p); |
| 103 | 103 | ||
| 104 | return true; // successfully removed! | 104 | return true; // successfully removed! |
| 105 | } | 105 | } |
| 106 | prev = p; | 106 | prev = p; |
| 107 | } | 107 | } |
| @@ -134,27 +134,28 @@ hash_free(struct hash_tab *ht) | |||
| 134 | } | 134 | } |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | int getword(FILE *fp, char *buf, int size, int first(int c), int rest(int c)) | 137 | int |
| 138 | getword(FILE *fp, char *buf, int size, int first(int), int rest(int)) | ||
| 138 | { | 139 | { |
| 139 | int i = 0, c; | 140 | int i = 0, c; |
| 140 | 141 | ||
| 141 | c = getc(fp); | 142 | c = getc(fp); |
| 142 | for ( ; c != EOF; c = getc(fp) ) | 143 | for ( ; c != EOF; c = getc(fp) ) |
| 143 | if ( first(c) ) { | 144 | if ( first(c) ) { |
| 144 | if ( i < size-1 ) | 145 | if ( i < size - 1 ) |
| 145 | buf[i++] = c; | 146 | buf[i++] = c; |
| 146 | c = getc(fp); | 147 | c = getc(fp); |
| 147 | break; | 148 | break; |
| 148 | } | 149 | } |
| 149 | 150 | ||
| 150 | for ( ; c != EOF && rest(c); c = getc(fp) ) | 151 | for ( ; c != EOF && rest(c); c = getc(fp) ) |
| 151 | if ( i < size-1) | 152 | if ( i < size - 1 ) |
| 152 | buf[i++] = c; | 153 | buf[i++] = c; |
| 153 | 154 | ||
| 154 | if ( i < size ) | 155 | if ( i < size ) |
| 155 | buf[i] = 0; | 156 | buf[i] = 0; |
| 156 | else | 157 | else |
| 157 | buf[size-1] = 0; | 158 | buf[size - 1] = 0; |
| 158 | 159 | ||
| 159 | if ( c != EOF ) | 160 | if ( c != EOF ) |
| 160 | ungetc(c, fp); | 161 | ungetc(c, fp); |
| @@ -162,15 +163,20 @@ int getword(FILE *fp, char *buf, int size, int first(int c), int rest(int c)) | |||
| 162 | return c > 0; | 163 | return c > 0; |
| 163 | } | 164 | } |
| 164 | 165 | ||
| 165 | int first(int c) { | 166 | int |
| 167 | first(int c) | ||
| 168 | { | ||
| 166 | return isalpha(c); | 169 | return isalpha(c); |
| 167 | } | 170 | } |
| 168 | 171 | ||
| 169 | int rest(int c) { | 172 | int |
| 173 | rest(int c) | ||
| 174 | { | ||
| 170 | return isalpha(c) || c == '_'; | 175 | return isalpha(c) || c == '_'; |
| 171 | } | 176 | } |
| 172 | 177 | ||
| 173 | void print(const char *key, T data, void *cl) | 178 | void |
| 179 | print(const char *key, T data, void *cl) | ||
| 174 | { | 180 | { |
| 175 | fprintf(cl, "%s: %d\n", key, data); | 181 | fprintf(cl, "%s: %d\n", key, data); |
| 176 | } | 182 | } |
| @@ -179,7 +185,7 @@ int | |||
| 179 | main() | 185 | main() |
| 180 | { | 186 | { |
| 181 | struct hash_tab ht[1]; | 187 | struct hash_tab ht[1]; |
| 182 | char word[100]; | 188 | char word[100]; |
| 183 | 189 | ||
| 184 | hash_init(ht); | 190 | hash_init(ht); |
| 185 | 191 | ||
| @@ -195,4 +201,3 @@ main() | |||
| 195 | 201 | ||
| 196 | hash_free(ht); | 202 | hash_free(ht); |
| 197 | } | 203 | } |
| 198 | |||
| @@ -13,7 +13,7 @@ typedef int T; | |||
| 13 | 13 | ||
| 14 | struct list_item { | 14 | struct list_item { |
| 15 | struct list_item *next; | 15 | struct list_item *next; |
| 16 | T data; | 16 | T data; |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | struct list_item * | 19 | struct list_item * |
| @@ -39,7 +39,7 @@ list_delete(struct list_item *list, T data) | |||
| 39 | 39 | ||
| 40 | for ( struct list_item *p = list; p; p = p->next ) { | 40 | for ( struct list_item *p = list; p; p = p->next ) { |
| 41 | if ( p->data == data ) { | 41 | if ( p->data == data ) { |
| 42 | if ( prev == NULL ) { /* first element in list? */ | 42 | if ( prev == NULL ) { /* first element in list? */ |
| 43 | list = p->next; | 43 | list = p->next; |
| 44 | } | 44 | } |
| 45 | else { | 45 | else { |
| @@ -70,13 +70,13 @@ list_length(struct list_item *list) | |||
| 70 | struct list_item * | 70 | struct list_item * |
| 71 | list_copy(struct list_item *list) | 71 | list_copy(struct list_item *list) |
| 72 | { | 72 | { |
| 73 | struct list_item *head, **p = &head; | 73 | struct list_item *head = NULL, **p = &head; |
| 74 | 74 | ||
| 75 | for ( ; list; list = list->next ) { | 75 | for ( ; list; list = list->next ) { |
| 76 | *p = malloc(sizeof **p); | 76 | *p = malloc(sizeof **p); |
| 77 | if ( *p != NULL ) { | 77 | if ( *p != NULL ) { |
| 78 | (*p)->data = list->data; // copy elements | 78 | (*p)->data = list->data; // copy elements |
| 79 | p = &(*p)->next; | 79 | p = &(*p)->next; |
| 80 | } | 80 | } |
| 81 | else | 81 | else |
| 82 | ERROR("out of memory"); | 82 | ERROR("out of memory"); |
| @@ -91,9 +91,9 @@ list_reverse(struct list_item *list) | |||
| 91 | struct list_item *head = NULL, *next; | 91 | struct list_item *head = NULL, *next; |
| 92 | 92 | ||
| 93 | for ( ; list; list = next ) { | 93 | for ( ; list; list = next ) { |
| 94 | next = list->next; | 94 | next = list->next; |
| 95 | list->next = head; | 95 | list->next = head; |
| 96 | head = list; | 96 | head = list; |
| 97 | } | 97 | } |
| 98 | return head; | 98 | return head; |
| 99 | } | 99 | } |
| @@ -109,7 +109,7 @@ list_apply(struct list_item *list, void (*visit)(T data, void *cl), void *cl) | |||
| 109 | struct list_item * | 109 | struct list_item * |
| 110 | list_merge(struct list_item *a, struct list_item *b) | 110 | list_merge(struct list_item *a, struct list_item *b) |
| 111 | { | 111 | { |
| 112 | struct list_item dummy = { .next = NULL }; | 112 | struct list_item dummy = { .next = NULL }; |
| 113 | struct list_item *head = &dummy, *c = head; | 113 | struct list_item *head = &dummy, *c = head; |
| 114 | 114 | ||
| 115 | while ( a != NULL && b != NULL ) | 115 | while ( a != NULL && b != NULL ) |
| @@ -118,7 +118,7 @@ list_merge(struct list_item *a, struct list_item *b) | |||
| 118 | else | 118 | else |
| 119 | c->next = b, c = b, b = b->next; | 119 | c->next = b, c = b, b = b->next; |
| 120 | 120 | ||
| 121 | c->next = ( a != NULL ) ? a : b; | 121 | c->next = (a != NULL) ? a : b; |
| 122 | 122 | ||
| 123 | return head->next; | 123 | return head->next; |
| 124 | } | 124 | } |
| @@ -130,7 +130,7 @@ list_sort(struct list_item *c) | |||
| 130 | return c; | 130 | return c; |
| 131 | 131 | ||
| 132 | struct list_item *a = c, | 132 | struct list_item *a = c, |
| 133 | *b = c->next; | 133 | *b = c->next; |
| 134 | 134 | ||
| 135 | while ( b != NULL && b->next != NULL ) | 135 | while ( b != NULL && b->next != NULL ) |
| 136 | c = c->next, b = b->next->next; | 136 | c = c->next, b = b->next->next; |
| @@ -151,7 +151,11 @@ list_free(struct list_item *list) | |||
| 151 | } | 151 | } |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | static void print_data(T data, void *cl) { fprintf(cl, "%d\n", data); } | 154 | static void |
| 155 | print_data(T data, void *cl) | ||
| 156 | { | ||
| 157 | fprintf(cl, "%d\n", data); | ||
| 158 | } | ||
| 155 | 159 | ||
| 156 | int | 160 | int |
| 157 | main() | 161 | main() |
| @@ -183,13 +187,13 @@ main() | |||
| 183 | struct list_item *x = NULL; | 187 | struct list_item *x = NULL; |
| 184 | for ( int i = 0; i != COUNT; ++i ) { | 188 | for ( int i = 0; i != COUNT; ++i ) { |
| 185 | int r = rand(); | 189 | int r = rand(); |
| 186 | x = list_add(x, r); | 190 | x = list_add(x, r); |
| 187 | } | 191 | } |
| 188 | 192 | ||
| 189 | puts("start"); | 193 | puts("start"); |
| 190 | start = clock(); | 194 | start = clock(); |
| 191 | x = list_sort(x); | 195 | x = list_sort(x); |
| 192 | printf("Fertig: %.3lf sec\n", (double)(clock() - start) / CLOCKS_PER_SEC); | 196 | printf("Fertig: %.3lf sec\n", (double) (clock() - start) / CLOCKS_PER_SEC); |
| 193 | 197 | ||
| 194 | //list_apply(x, print_data); | 198 | //list_apply(x, print_data); |
| 195 | printf("Len: %zu\n", list_length(x)); | 199 | printf("Len: %zu\n", list_length(x)); |
| @@ -198,6 +202,3 @@ main() | |||
| 198 | 202 | ||
| 199 | return EXIT_SUCCESS; | 203 | return EXIT_SUCCESS; |
| 200 | } | 204 | } |
| 201 | |||
| 202 | |||
| 203 | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | /* | ||
| 2 | Implementierung übernommen von: https://web.archive.org/web/20140328232325/http://en.literateprograms.org/Red-black_tree_(C) | ||
| 3 | */ | ||
| 4 | |||
| 1 | #include <stdio.h> | 5 | #include <stdio.h> |
| 2 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 3 | #include <time.h> | 7 | #include <time.h> |
