4 * @brief Generic Double Linked List
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <pluto/constants.h>
26 #include <pluto/defs.h>
28 #include "linked_list.h"
32 * @brief implements function destroy of linked_list_item_t
34 static status_t
destroy_linked_list_element(linked_list_element_t
*linked_list_element
)
36 linked_list_element_t
* this = linked_list_element
;
41 linked_list_element_t
*linked_list_element_create(void *value
)
43 linked_list_element_t
*this = alloc_thing(linked_list_element_t
, "linked_list_element_t");
45 this->destroy
= destroy_linked_list_element
;
55 * @brief implements function insert_first of linked_list_t
57 static status_t
insert_first(linked_list_t
*linked_list
, void *item
)
59 linked_list_t
*this = linked_list
;
61 linked_list_element_t
*element
= linked_list_element_create(item
);
65 /* first entry in list */
66 this->first
= element
;
68 element
->previous
= NULL
;
72 if ((this->first
== NULL
) || (this->last
== NULL
))
74 /* should never happen */
75 element
->destroy(element
);
78 linked_list_element_t
*old_first_element
= this->first
;
79 element
->next
= old_first_element
;
80 old_first_element
->previous
= element
;
81 this->first
= element
;
90 * @brief implements function remove_first of linked_list_t
92 static status_t
remove_first(linked_list_t
*linked_list
, void **item
)
94 linked_list_t
*this = linked_list
;
101 if (this->first
== NULL
)
106 linked_list_element_t
*element
= this->first
;
108 if (element
->next
!= NULL
)
110 element
->next
->previous
= NULL
;
112 this->first
= element
->next
;
114 *item
= element
->value
;
118 return element
->destroy(element
);
122 * @brief implements function get_first of linked_list_t
124 static status_t
get_first(linked_list_t
*linked_list
, void **item
)
126 linked_list_t
*this = linked_list
;
128 if (this->count
== 0)
133 if (this->first
== NULL
)
138 *item
= this->first
->value
;
144 * @brief implements function insert_last of linked_list_t
146 static status_t
insert_last(linked_list_t
*linked_list
, void *item
)
148 linked_list_t
*this = linked_list
;
150 linked_list_element_t
*element
= linked_list_element_create(item
);
152 if (this->count
== 0)
154 /* first entry in list */
155 this->first
= element
;
156 this->last
= element
;
157 element
->previous
= NULL
;
158 element
->next
= NULL
;
161 if ((this->first
== NULL
) || (this->last
== NULL
))
163 /* should never happen */
164 element
->destroy(element
);
167 linked_list_element_t
*old_last_element
= this->last
;
168 element
->previous
= old_last_element
;
169 old_last_element
->next
= element
;
170 this->last
= element
;
179 * @brief implements function remove_last of linked_list_t
181 static status_t
remove_last(linked_list_t
*linked_list
, void **item
)
183 linked_list_t
*this = linked_list
;
185 if (this->count
== 0)
190 if (this->last
== NULL
)
195 linked_list_element_t
*element
= this->last
;
197 if (element
->previous
!= NULL
)
199 element
->previous
->next
= NULL
;
201 this->last
= element
->previous
;
203 *item
= element
->value
;
207 return element
->destroy(element
);
211 * @brief implements function get_last of linked_list_t
213 static status_t
get_last(linked_list_t
*linked_list
, void **item
)
215 linked_list_t
*this = linked_list
;
217 if (this->count
== 0)
222 if (this->last
== NULL
)
227 *item
= this->last
->value
;
233 * @brief implements function destroy of linked_list_t
235 static status_t
destroy_linked_list(linked_list_t
*linked_list
)
237 linked_list_t
*this = linked_list
;
239 /* Delete all list items before deleting list */
240 while (this->count
> 0)
243 if (this->remove_first(this,&value
) != SUCCESS
)
254 linked_list_t
*linked_list_create()
256 linked_list_t
*this = alloc_thing(linked_list_t
, "linked_list_t");
258 this->get_first
= get_first
;
259 this->get_last
= get_last
;
260 this->insert_first
= insert_first
;
261 this->insert_last
= insert_last
;
262 this->remove_first
= remove_first
;
263 this->remove_last
= remove_last
;
264 this->destroy
= destroy_linked_list
;