-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlist.c
70 lines (62 loc) · 1.04 KB
/
list.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "list.h"
#include "gc.h"
list *
newitem(void *v) {
list *o = malloc(sizeof(list));
if (o == NULL) {
exit(1);
}
o->val = v;
o->next = NULL;
gc_register((void *)o, LIST);
return o;
}
list *
copyitem(list *i) {
list *o = malloc(sizeof(list));
if (o == NULL) {
exit(1);
}
o->val = i->val;
o->next = NULL;
return o;
}
list *
append(list *l, void *v) {
list *ni = newitem(v);
if (l == NULL) {
return ni;
}
list *curr;
for (curr = l; curr->next != NULL; curr = curr->next)
;
curr->next = ni;
return l;
}
list *
concat(list *h, list *t) {
list *curr;
for (curr = h; curr->next != NULL; curr = curr->next)
;
curr->next = t;
return h;
}
//shallow copy
list *
copy(list *l) {
list *o = NULL;
list *curr;
for (curr = l; curr != NULL; curr = curr->next) {
o = append(o, curr->val);
}
return o;
}
//objs flag set to true will also free the objects in the list
void
list_free(void *_l) {
list *l = _l;
free(l);
}