2
* yahoo_list.c: linked list routines
4
* Some code copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
5
* Other code copyright Meredydd Luff <meredydd AT everybuddy.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* Some of this code was borrowed from elist.c in the eb-lite sources
27
#include "yahoo_list.h"
29
YList *y_list_append(YList * list, void *data)
32
YList *new_list = malloc(sizeof(YList));
33
YList *attach_to = NULL;
35
new_list->next = NULL;
36
new_list->data = data;
38
for (n = list; n != NULL; n = n->next) {
42
if (attach_to == NULL) {
43
new_list->prev = NULL;
46
new_list->prev = attach_to;
47
attach_to->next = new_list;
52
YList *y_list_prepend(YList * list, void *data)
54
YList *n = malloc(sizeof(YList));
65
YList *y_list_concat(YList * list, YList * add)
75
for (l = list; l->next; l = l->next)
84
YList *y_list_remove(YList * list, void *data)
88
for (n = list; n != NULL; n = n->next) {
89
if (n->data == data) {
90
list=y_list_remove_link(list, n);
100
/* link MUST be part of list */
101
/* caller must free link using y_list_free_1 */
102
YList *y_list_remove_link(YList * list, const YList * link)
108
link->next->prev = link->prev;
110
link->prev->next = link->next;
118
int y_list_length(const YList * list)
121
const YList *n = list;
123
for (n = list; n != NULL; n = n->next) {
130
/* well, you could just check for list == NULL, but that would be
131
* implementation dependent
133
int y_list_empty(const YList * list)
141
int y_list_singleton(const YList * list)
143
if(!list || list->next)
148
YList *y_list_copy(YList * list)
153
for (n = list; n != NULL; n = n->next) {
154
copy = y_list_append(copy, n->data);
160
void y_list_free_1(YList * list)
165
void y_list_free(YList * list)
170
YList *next = n->next;
176
YList *y_list_find(YList * list, const void *data)
179
for (l = list; l && l->data != data; l = l->next)
185
void y_list_foreach(YList * list, YListFunc fn, void * user_data)
187
for (; list; list = list->next)
188
fn(list->data, user_data);
191
YList *y_list_find_custom(YList * list, const void *data, YListCompFunc comp)
194
for (l = list; l; l = l->next)
195
if (comp(l->data, data) == 0)
201
YList *y_list_nth(YList * list, int n)
204
for ( ; list && i; list = list->next, i--)
210
YList *y_list_insert_sorted(YList * list, void *data, YListCompFunc comp)
212
YList *l, *n, *prev = NULL;
214
return y_list_append(list, data);
216
n = malloc(sizeof(YList));
218
for (l = list; l && comp(l->data, n->data) <= 0; l = l->next)