updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / var / da / makedepf90-2.8.8 / list.c
blobe187085e154632828644f5c596dfde6fa359bd7f
1 /*
2 * Copyright (C) 2000-2005 Erik Edelmann <Erik.Edelmann@iki.fi>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License version 2 as published by the Free Software
7 * Foundation.
9 * This program is distributed in the hope that it will be
10 * useful, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 * PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the Free
17 * Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
21 #include <stdlib.h>
22 #include <assert.h>
23 #include "list.h"
24 #include "xmalloc.h"
27 List *list_find (List *l, const void *data,
28 int (*cmpfunc)(const void *, const void *))
29 /*
30 * If data matching 's' is found, return it's address. If no such data is
31 * found, return NULL.
33 * 'cmpfunc' should return 0 for equal, non 0 for unequal
36 List *h;
38 if (!l) return NULL;
40 for (h = l; h; h = h->next)
41 if (cmpfunc(data, h->data) == 0) return h;
43 return NULL;
47 List *list_prepend (List *l, void *data)
48 /*
49 * Prepend 'data' to 'l', return address to the updated list.
52 List *new;
54 new = (List *) xmalloc(sizeof(List));
55 new->data = data;
56 new->next = l;
57 return new;
61 List *list_append (List *l, void *data)
62 /*
63 * Append 'data' to 'l', return address to the updated list. This function is
64 * slower than 'list_prepend', use only when ordering is important.
67 List *new, *h;
69 new = (List *) xmalloc(sizeof(List));
70 new->data = data;
71 new->next = NULL;
73 if (l == NULL)
74 return new;
75 else {
76 for (h = l; h->next; h = h->next) ;
77 h->next = new;
78 return l;
83 void list_free (List *l)
85 List *h;
87 while (l) {
88 h = l->next;
89 free (l);
90 l = h;
95 List *list_remove (List *l, List *node)
96 /* Remove 'node' from 'l'. Return address of the new updated list */
98 List *h, *hold;
100 if (!l) return NULL;
101 if (!node) return l;
103 hold = NULL;
104 h = l;
105 while (h != node && h != NULL) {
106 hold = h;
107 h = h->next;
110 assert (h != NULL);
112 if (hold == NULL)
113 l = h->next;
114 else
115 hold->next = h->next;
117 node->next = NULL;
119 return l;
123 int list_length (const List *l)
125 int n = 0;
126 const List *h;
128 for (h = l; h; h = h->next) n++;
130 return n;