File: llist.c

package info (click to toggle)
vtun 3.0.4-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,520 kB
  • sloc: ansic: 4,180; sh: 2,814; yacc: 536; lex: 195; makefile: 139
file content (102 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download
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
101
102

/*  
    VTun - Virtual Tunnel over TCP/IP network.

    Copyright (C) 1998-2016  Maxim Krasnyansky <max_mk@yahoo.com>

    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.3 2016/10/01 21:46:01 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;
}