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
103
104
105
106
107
108
109
110
111
112
|
#ifndef __H_LINKED_LIST__
#define __H_LINKED_LIST__
#include <stdlib.h>
#include <stdbool.h>
/**
* This file, as the rest of the project is under MIT license.
* see http://opensource.org/licenses/MIT
*
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
*/
/** @file
* A LinkedList represents a linked list. A linked list is an efficient data
* structure. It is not posible to index a linked list.
*/
/**
* Represents an object in a LinkedList.
*/
typedef struct LinkedListNode {
LinkedListNode * prev;
LinkedListNode * next;
void * data;
} LinkedListNode;
/**
* A linked list is a data stucture that holds information in a non-idexible
* way. It is good when the objects in the list in verry mutable, but has a
* linear search time.
*/
typedef struct LinkedList {
size_t len; /**< The length of the list, may be wrong.*/
void (* free_func)(void *); /**< The function that frees the data. */
LinkedListNode * head; /**< head->prev must be NULL */
LinkedListNode * tail; /**< tail->next must be NULL */
LinkedListNode * current; /**< Represents the curret object*/
} LinkedList;
/**
* Create a new linked list.
*
* @param free_func the function to be used when freeing a linked list,
* if it is NULL the free func will be set to the standard free() function.
*/
LinkedList * linked_list_new (void (* free_func)(void *));
/**
* frees a linked list.
*
* @param self the linked list to free.
* @param free_data default:true, wether or not to free the data in the list
* as well as the list.
*/
void linked_list_free (LinkedList * self, bool free_data = true);
/**
* Add an item to a linked list.
*
* @param self the list to add an itme to.
* @param data the data to be stored.
*/
void linked_list_add (LinkedList * self, void * data);
/**
* Iterate to the next item. This function does not return anything.
*
* @param self the list to iterate over.
*/
void linked_list_next (LinkedList * self);
/**
* Iterate to the provious item. This function does not return anything.
*
* @param self the list to iterate over.
*/
void linked_list_pev (LinkedList * self);
/**
* Get the current item.
*/
void * linked_list_get_current (LinkedList * self);
/**
* Get the next item.
*/
void * linked_list_get_next (LinkedList * self);
/**
* Get previous item.
*/
void * linked_list_get_prev (LinkedList * self);
/**
* Wind the list to the head (start).
*/
void linked_list_head (LinkedList * self);
/**
* Wind the the list to the tail (end).
*/
void linked_list_tail (LinkedList * self);
/**
* Get the length of the list.
*
* @warning the value may be wrong.
*/
size_t linked_list_len (LinkedList * self);
#endif /* __H_LINKED_LIST__ */
|