4 Copyright (c) 2007, Matthew Schmidt <matt.alboin@gmail.com>
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "include/alboin.h"
23 #include "include/ll.h"
26 ll
*l
= malloc(sizeof(ll
));
29 l
->fe_started
= false;
31 l
->base
= malloc(sizeof(linked_list
));
37 void ll_add(ll
*l
, void *a
) {
42 l
->current
->next
= malloc(sizeof(ll
));
43 l
->current
->next
->parent
= l
->current
;
44 l
->current
= l
->current
->next
;
45 l
->current
->next
= NULL
;
48 void *ll_remove(ll
*l
, int item
) {
51 if(count
== (item
-1)) {
52 linked_list
*rem
= l
->current
->next
;
53 l
->current
->next
= rem
->next
;
65 /* Free the structure. */
66 if(l
->current
->next
== NULL
) {
71 linked_list
*tmp
= l
->current
->next
;
78 void ll_switch(linked_list
*l1
, linked_list
*l2
) {
86 void ll_sort(ll
*l
, int (*comp
)(void*, void*)) {
94 int r
= comp(l
->current
->data
, l
->current
->next
->data
);
96 ll_switch(l
->current
, l
->current
->next
);
106 void ll_reset(ll
*l
) {
107 l
->current
= l
->base
;
110 void *ll_foreach(ll
*l
) {
113 /* We have to reset if we haven't already. */
116 l
->fe_started
= true;
122 ret
= l
->current
->data
;
124 if(l
->current
->next
!= NULL
) {
125 l
->current
= l
->current
->next
;
126 l
->next_state
= true;
129 /* We're finished here. Wrap it all up. */
131 l
->fe_started
= false;
132 l
->next_state
= false;