Changed all the licence notices in all the files.
[pspp-builds.git] / src / linked-list.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "alloc.h"
25 #include "linked-list.h"
26
27 /* Iteration */
28
29 /* Return the first element in LL */
30 void *
31 ll_first (const struct linked_list *ll, struct ll_iterator *li)
32 {
33   assert(ll); 
34
35   li->p = ll->head;
36
37   return ll->head->entry;
38 }
39
40 /* Return the next element in LL iterated by LI */
41 void *
42 ll_next (const struct linked_list *ll, struct ll_iterator *li)
43 {
44   assert( ll ) ;
45
46   li->p = li->p->next;
47
48   if ( ! li->p ) 
49     return 0;
50
51   return li->p->entry;
52 }
53
54
55 /* Create a linked list.
56    Elements will be freed using F and AUX
57 */
58 struct linked_list *
59 ll_create( ll_free_func *f , void *aux)
60 {
61   struct linked_list *ll = xmalloc ( sizeof(struct linked_list) ) ;
62
63   ll->head = 0;
64   ll->free = f;
65   ll->aux  = aux;
66
67   return ll;
68 }
69
70
71 /* Destroy a linked list */
72 void
73 ll_destroy(struct linked_list *ll)
74 {
75   struct node *n = ll->head;
76
77   while (n)
78     {
79       struct node *nn = n->next;
80       if ( ll->free ) 
81         ll->free(n->entry, ll->aux);
82       free (n);
83       n = nn;
84     }
85
86   free (ll);
87 }
88
89
90 /* Push a an element ENTRY onto the list LL */
91 void
92 ll_push_front(struct linked_list *ll, void *entry)
93 {
94   struct node *n ; 
95   assert (ll);
96
97   n = xmalloc (sizeof(struct node) );
98   n->next = ll->head;
99   n->entry = entry;
100   ll->head = n;
101 }
102