-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.c
79 lines (68 loc) · 1.44 KB
/
llist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "llist.h"
#include <stdio.h>
#include <stdlib.h>
list_t* list_create(node_copy_func copy_func, node_free_func free_func) {
list_t *l = malloc(sizeof(list_t));
if(!l) {
perror("malloc");
return NULL;
}
l->first = NULL;
l->last = NULL;
l->copy_func = copy_func;
l->free_func = free_func;
return l;
}
void _list_prepend(list_t *l, node_t *n) {
n->next = l->first;
l->first = n;
if(!l->last)
l->last = n;
}
void _list_append(list_t *l, node_t *n) {
if(!l->first) {
l->first = n;
l->last = n;
} else {
l->last->next = n;
l->last = n;
}
n->next = NULL;
}
list_t* list_union(list_t *l1, list_t *l2) {
if(!l1->last)
l1->first = l2->first;
else
l1->last->next = l2->first;
l1->last = l2->last;
free(l2);
return l1;
}
list_t* list_copy(list_t *l) {
if(!l->copy_func)
return NULL;
list_t *cpy = list_create(l->copy_func, l->free_func);
if(!cpy)
goto exit;
list_for_each(i, node_t, l) {
node_t *n = l->copy_func(i);
if(!n)
goto exit;
list_append(cpy, n);
}
return cpy;
exit:
if(cpy) list_free(cpy);
return NULL;
}
void list_free(list_t *l) {
node_t *i = l->first, *tmp;
while(i != NULL) {
tmp = i->next;
if(l->free_func)
l->free_func(i);
free(i);
i = tmp;
}
free(l);
}