-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.c
102 lines (78 loc) · 1.96 KB
/
llist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
VTun - Virtual Tunnel over TCP/IP network.
Copyright (C) 1998-2008 Maxim Krasnyansky <[email protected]>
VTun has been derived from VPPP package by Maxim Krasnyansky.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
* $Id: llist.c,v 1.3.2.2 2008/01/07 22:35:47 mtbishop Exp $
*/
#include <stdlib.h>
#include "llist.h"
/* Function to work with the Linked Lists */
void llist_init(llist *l)
{
l->head = l->tail = NULL;
}
int llist_empty(llist *l)
{
return l->tail == NULL;
}
int llist_add(llist *l, void * d)
{
llist_elm *e;
if( !(e=malloc(sizeof(llist_elm))) )
return -1;
if( !l->head )
l->head = l->tail = e;
else
l->tail->next = e;
l->tail = e;
e->next = NULL;
e->data = d;
return 0;
}
/* Travel list from head to tail */
void * llist_trav(llist *l, int (*f)(void *d, void *u), void *u)
{
llist_elm *i = l->head;
while( i ){
if( f(i->data,u) ) return i->data;
i = i->next;
}
return NULL;
}
/* Copy list from (l) to (t) */
int llist_copy(llist *l, llist *t, void* (*f)(void *d, void *u), void *u)
{
llist_elm *i = l->head;
llist_init(t);
while( i ){
llist_add(t,f(i->data,u));
i = i->next;
}
return 0;
}
/* Travel list from head to tail, deallocate each element */
void * llist_free(llist *l, int (*f)(void *d, void *u), void *u)
{
llist_elm *i = l->head, *n;
void *ff = NULL;
while( i ){
n = i->next;
if( f(i->data,u) )
ff = i->data;
else
free(i);
i = n;
}
l->head = l->tail = NULL;
return ff;
}