1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 * $Id: list.c 2096 2001-07-31 01:00:39Z warmenhoven $
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
7 * Bill Soudan <soudan@kde.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * linked list functions
34 icq_List
*icq_ListNew()
36 icq_List
*plist
=(icq_List
*)malloc(sizeof(icq_List
));
45 /* Frees all list nodes and list itself */
46 void icq_ListDelete(icq_List
*plist
, void (*item_free_f
)(void *))
49 icq_ListFree(plist
, item_free_f
);
53 /* Only frees the list nodes */
54 void icq_ListFree(icq_List
*plist
, void (*item_free_f
)(void *))
56 icq_ListNode
*p
=plist
->head
;
59 printf("icq_ListFree(%p)\n", plist
);
65 icq_ListNode
*ptemp
=p
;
68 (*item_free_f
)((void *)ptemp
->item
);
69 icq_ListRemoveNode(plist
, ptemp
);
73 void icq_ListInsertSorted(icq_List
*plist
, void *pitem
)
75 icq_ListNode
*i
=plist
->head
;
80 if ((*plist
->compare_function
)(pitem
, i
->item
)<0)
86 icq_ListInsert(plist
, i
, pitem
);
89 void icq_ListInsert(icq_List
*plist
, icq_ListNode
*pnode
, void *pitem
)
91 icq_ListNode
*pnew
=(icq_ListNode
*)malloc(sizeof(icq_ListNode
));
95 printf("inserting %x (node=%x) into list %x\n", pitem
, pnew
, plist
);
100 /* null source node signifies insert at end of icq_List */
103 pnew
->previous
=plist
->tail
;
106 plist
->tail
->next
=pnew
;
114 pnew
->previous
=pnode
->previous
;
118 pnew
->previous
->next
=pnew
;
121 pnode
->previous
=pnew
;
123 if(plist
->head
==pnode
)
132 void *icq_ListRemoveNode(icq_List
*plist
, icq_ListNode
*p
)
140 printf("removing %x (node=%x) from list %x\n", p
->item
, p
, plist
);
146 p
->next
->previous
=p
->previous
;
149 p
->previous
->next
=p
->next
;
155 plist
->tail
=p
->previous
;
171 void *icq_ListTraverse(icq_List
*plist
, int (*item_f
)(void *, va_list), ...)
173 icq_ListNode
*i
=plist
->head
;
178 printf("icq_ListTraverse(%p)\n", plist
);
181 va_start(ap
, item_f
);
183 /* call item_f for each item in list until end of list or item
184 * function returns 0 */
187 icq_ListNode
*pnext
=i
->next
;
189 if(!(f
=(*item_f
)(i
->item
, ap
)))
201 int icq_ListDump(icq_List
*plist
)
203 icq_ListNode
*p
=plist
->head
;
205 printf("list %lx { head=%lx, tail=%lx, count=%d }\ncontents: ",
206 (long)plist
, (long)plist
->head
, (long)plist
->tail
, plist
->count
);
210 printf("%lx, ", (long)p
->item
);
218 void *icq_ListFirst(icq_List
*plist
)
221 return plist
->head
->item
;
226 void *icq_ListLast(icq_List
*plist
)
229 return plist
->tail
->item
;
234 void *icq_ListAt(icq_List
*plist
, int num
)
236 icq_ListNode
*ptr
= plist
->head
;
248 icq_ListNode
*icq_ListFind(icq_List
*plist
, void *pitem
)
250 icq_ListNode
*p
=plist
->head
;
261 void *icq_ListRemove(icq_List
*plist
, void *pitem
)
263 icq_ListNode
*p
=icq_ListFind(plist
, pitem
);
266 return icq_ListRemoveNode(plist
, p
);