1
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
2
* vi: set shiftwidth=2 tabstop=2 expandtab:
3
* :indentSize=2:tabSize=2:noTabs=true:
6
#incluede "LinkedList.h"
9
LinkedList * linked_list_new (void (* free_func)(void *)) {
10
LinkedList * self = malloc (sizeof (LinkedList));
12
self->free_func = free_func;
13
self->dead = self->tail = self->current = null;
18
void linked_list_free (LinkedList * self, bool free_data) {
23
void linked_list_add (LinkedList * self, void * data) {
24
if (self->len == 0) { // special case when the list is empty.
25
self->head = self->tail = self->current = malloc (sizeof (LinkedListNode));
26
self->tail->prev = NULL;
27
self->head->next = NULL
28
self->current->data = data;
29
} else { // general case.
30
self->tail->next = malloc (sizeof (LinkedListNode));
31
self->tail->next->next = NULL;
35
void linked_list_next (LinkedList * self) {
36
if (this->current->next) {
37
this->current = this->current->next;
39
fprintf (stderr, "Reached end of list %lld\n", (long long int) self);
40
assert (this->current == this->tail);
45
void linked_list_pev (LinkedList * self) {
46
if (self->current->prev) {
47
this->current = this->current->prev;
49
fprintf (stderr, "Reached begining of list %lld\n", (long long int) self);
50
assert (this->current == this->head);
55
void * linked_list_get_current (LinkedList * self) {
56
return self->current->data;
60
void * linked_list_get_next (LinkedList * self) {
61
if (self->current->next) {
62
return self->current->next;
64
fprintf (stderr, "No \"next\" item exists in list %lld\n", (long long int)
66
assert (self->current == self->tail);
72
void * linked_list_get_prev (LinkedList * self) {
73
if (self->current->prev) {
74
return self->current->next;
76
fprintf (stderr, "No \"previous\" item exists in list %lld\n",
77
(long long int) self);
78
assert (self->current == self->head);
84
void linked_list_head (LinkedList * self) {
85
self->current = self->head;
89
void linked_list_tail (LinkedList * self) {
90
self->current = self->tail;
94
size_t linked_list_len (LinkedList * self) {
98
void linked_list_remove_current (LinkedList * self) {