aboutsummaryrefslogtreecommitdiff
path: root/linked_list.inc.h
blob: 260eaf0dc9d36fa6a31316d829ef7528d622f7e3 (plain)
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
#ifndef TYPE
#error "Set TYPE before including this file"
#else

int CONSTRUCTOR_DECL ( LLIST(TYPE) ) {
	this->length = 0;
	this->head = NULL;
	this->tail = NULL;
	this->cur = NULL;
	return 0;
}

int LLIST_FREE(TYPE) (LLIST(TYPE)* this ) {
	LINK(TYPE) *node, *next;
	node = this->head;
	while (node != NULL) {
		next = node->after;
		FFREE(TYPE, node->value);
		free(node);
		node = next;
	}
	this->length = 0;
	return 0;
}

int CONSTRUCTOR_DECL ( LINK(TYPE) ) {
	this->before = NULL;
	this->after  = NULL;
	this->value  = NULL;
	return 0;
}

int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val) {
	NEW(LINK(TYPE), l);
	l->after = lst->head;
	if (l->after != NULL) {
		l->after->before = l;
	}
	lst->head = l;
	l->value = val;
	++lst->length;

	lst->cur = l;

	return 0;
}

int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
	LINK(TYPE)* n = src->head;

	while (n != NULL) {
		NEW(TYPE, cpy);
		DEEP_COPY(TYPE)(cpy, n->value);
		LLIST_CONS(TYPE) ( dest, cpy );
		n = n->after;
	}

	return 0;
}

#endif /* TYPE */