aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hashtab.c51
-rw-r--r--list.c35
-rw-r--r--rb.c4
3 files changed, 50 insertions, 40 deletions
diff --git a/hashtab.c b/hashtab.c
index 6b35c89..e53da51 100644
--- a/hashtab.c
+++ b/hashtab.c
@@ -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
11struct hash_item { 11struct 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
17struct hash_tab { 17struct 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
21static unsigned long 21static unsigned long
22hash_key(const unsigned char *str) 22hash_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 *
41hash_add(struct hash_item *next, const char *key, T data) 41hash_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)
63T * 63T *
64hash_lookup(struct hash_tab *ht, const char *key, T data, int create) 64hash_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)
86bool 86bool
87hash_delete(struct hash_tab *ht, const char *key) 87hash_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
137int getword(FILE *fp, char *buf, int size, int first(int c), int rest(int c)) 137int
138getword(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
165int first(int c) { 166int
167first(int c)
168{
166 return isalpha(c); 169 return isalpha(c);
167} 170}
168 171
169int rest(int c) { 172int
173rest(int c)
174{
170 return isalpha(c) || c == '_'; 175 return isalpha(c) || c == '_';
171} 176}
172 177
173void print(const char *key, T data, void *cl) 178void
179print(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
179main() 185main()
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
diff --git a/list.c b/list.c
index 8b18de8..18465b0 100644
--- a/list.c
+++ b/list.c
@@ -13,7 +13,7 @@ typedef int T;
13 13
14struct list_item { 14struct list_item {
15 struct list_item *next; 15 struct list_item *next;
16 T data; 16 T data;
17}; 17};
18 18
19struct list_item * 19struct 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)
70struct list_item * 70struct list_item *
71list_copy(struct list_item *list) 71list_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)
109struct list_item * 109struct list_item *
110list_merge(struct list_item *a, struct list_item *b) 110list_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
154static void print_data(T data, void *cl) { fprintf(cl, "%d\n", data); } 154static void
155print_data(T data, void *cl)
156{
157 fprintf(cl, "%d\n", data);
158}
155 159
156int 160int
157main() 161main()
@@ -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
diff --git a/rb.c b/rb.c
index 2a8bff2..1a9e873 100644
--- a/rb.c
+++ b/rb.c
@@ -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>