aboutsummaryrefslogtreecommitdiff
path: root/list.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-08-02 10:38:41 +0200
committerThomas Schmucker <ts@its1.de>2020-08-02 10:38:41 +0200
commite7986e3e11b9ca91079bacae7fc2f79dce515f5d (patch)
tree8445b05e4b1f3f8c0dfeef5f9e6d023f52ad108d /list.c
parent1aecbf40567f0f1943285c774b4d6369e1a90ceb (diff)
downloaddata-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.tar.gz
data-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.tar.bz2
data-structures-e7986e3e11b9ca91079bacae7fc2f79dce515f5d.zip
format code
Diffstat (limited to 'list.c')
-rw-r--r--list.c35
1 files changed, 18 insertions, 17 deletions
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