2 * Copyright (c) 2007 The LOST Project. All rights reserved.
4 * This code is derived from software contributed to the LOST Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
34 * Creates a linked list
37 llist_t
llist_create() {
38 llist_t list
= malloc(sizeof(struct llist_S
));
45 * Destroys a linked list
46 * @param list linked list to destroy
48 void llist_destroy(llist_t list
) {
49 while (llist_pop(list
)!=NULL
);
54 * Gets size of a linked list
55 * @param list linked list to get size of
58 size_t llist_size(llist_t list
) {
59 if (!list
|| !list
->anchor
) return 0;
60 else return list
->size
;
64 * Push element to linked list
65 * @param list linked list to get size of
66 * @param value Value to add to list
69 llist_t
llist_push(llist_t list
,void *value
) {
70 if (!list
) return NULL
;
71 struct llist_node
*node
= malloc(sizeof(struct llist_node
));
73 node
->next
= list
->anchor
;
80 * Pops element from linked list
81 * @param list linked list to pop from
82 * @return Value popped
84 void* llist_pop(llist_t list
) {
85 struct llist_node
* old_anchor
;
88 if (!list
) return NULL
;
91 value
= list
->anchor
->value
;
92 old_anchor
= list
->anchor
;
93 list
->anchor
= list
->anchor
->next
;
103 * Gets element from a list
104 * @param list linked list to get element from
105 * @param index Index of element
108 struct llist_node
*llist_get_node_at(llist_t list
,size_t index
) {
109 if (!list
|| !list
->anchor
|| index
<0) return NULL
;
110 struct llist_node
* current
= list
->anchor
;
112 if (index
>list
->size
-1) return NULL
;
115 current
= current
->next
;
116 if (!current
) return NULL
;
123 * Gets value of a element in a list
124 * @param list linked list to get value from
125 * @param index Index of element to get value from
128 void *llist_get(llist_t list
,size_t index
) {
129 struct llist_node
*node
= llist_get_node_at(list
,index
);
130 if (node
==NULL
) return NULL
;
131 else return node
->value
;
135 * Inserts an element in a list
136 * @param list linked list to insert in
137 * @param index Index of new element
138 * @param value Value of new element
139 * @return linked list
141 llist_t
llist_insert(llist_t list
,size_t index
,void* value
) {
142 struct llist_node
* new_node
= malloc(sizeof(struct llist_node
));
143 new_node
->value
= value
;
145 if (!list
) return NULL
;
148 struct llist_node
* prev_node
= llist_get_node_at(list
,index
-1);
149 if (!prev_node
) return NULL
;
150 new_node
->next
= prev_node
->next
;
151 prev_node
->next
= new_node
;
154 new_node
->next
= list
->anchor
;
155 list
->anchor
= new_node
;
164 * Removes element from list
165 * @param list linked list to remove element from
166 * @param index Index of element to remove
167 * @return Value of removed element
169 void* llist_remove(llist_t list
, size_t index
) {
172 if (!list
) return NULL
;
174 struct llist_node
* node
;
176 struct llist_node
* prev_node
= llist_get_node_at(list
,index
-1);
178 if (!prev_node
|| !prev_node
->next
) return NULL
;
180 node
= prev_node
->next
;
181 prev_node
->next
= node
->next
;
186 if (!list
->anchor
) return NULL
;
189 list
->anchor
= node
->next
;;
200 * Finds element in list
201 * @param list Linked list to search in
202 * @param find Element to find
203 * @return Index of found Element
205 size_t llist_find(llist_t list
,void *find
) {
209 for (i
=0;(element
= llist_get(list
,i
));i
++) {
210 if (element
==find
) return i
;
217 * @param list Linked list to be copied
218 * @return New linked list
220 llist_t
llist_copy(llist_t list
) {
223 llist_t
new = llist_create();
224 for (i
=0;(element
= llist_get(list
,i
));i
++) llist_push(new,element
);