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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
* vi: set shiftwidth=2 tabstop=2 expandtab:
* :indentSize=2:tabSize=2:noTabs=true:
*/
#incluede "LinkedList.h"
LinkedList * linked_list_new (void (* free_func)(void *)) {
LinkedList * self = malloc (sizeof (LinkedList));
self->len = 0;
self->free_func = free_func;
self->dead = self->tail = self->current = null;
return self;
}
void linked_list_free (LinkedList * self, bool free_data) {
}
void linked_list_add (LinkedList * self, void * data) {
if (self->len == 0) { // special case when the list is empty.
self->head = self->tail = self->current = malloc (sizeof (LinkedListNode));
self->tail->prev = NULL;
self->head->next = NULL
self->current->data = data;
} else { // general case.
self->tail->next = malloc (sizeof (LinkedListNode));
self->tail->next->next = NULL;
}
}
void linked_list_next (LinkedList * self) {
if (this->current->next) {
this->current = this->current->next;
} else {
fprintf (stderr, "Reached end of list %lld\n", (long long int) self);
assert (this->current == this->tail);
}
}
void linked_list_pev (LinkedList * self) {
if (self->current->prev) {
this->current = this->current->prev;
} else {
fprintf (stderr, "Reached begining of list %lld\n", (long long int) self);
assert (this->current == this->head);
}
}
void * linked_list_get_current (LinkedList * self) {
return self->current->data;
}
void * linked_list_get_next (LinkedList * self) {
if (self->current->next) {
return self->current->next;
} else {
fprintf (stderr, "No \"next\" item exists in list %lld\n", (long long int)
self);
assert (self->current == self->tail);
return NULL;
}
}
void * linked_list_get_prev (LinkedList * self) {
if (self->current->prev) {
return self->current->next;
} else {
fprintf (stderr, "No \"previous\" item exists in list %lld\n",
(long long int) self);
assert (self->current == self->head);
return NULL;
}
}
void linked_list_head (LinkedList * self) {
self->current = self->head;
}
void linked_list_tail (LinkedList * self) {
self->current = self->tail;
}
size_t linked_list_len (LinkedList * self) {
return self->len;
}
void linked_list_remove_current (LinkedList * self) {
// TODO
}
|