Debian Codegraph @

list.c

raw symbols SCIP
1 /** @file list.c
2 Implementation of a doubly linked list.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 This program is released under the GPL with the additional exemption
19 that compiling, linking, and/or using OpenSSL is allowed.
20
21 Copyright (C) 2008 Bertrand Mesot <http://www.objectif-securite.ch>
22 */
23
24 #include <stdlib.h>
25 #include <assert.h>
26
27 #include "list.h"
28 /*-------------------------------------------------------------------------*/
29 list_t *list_alloc(void) {
30 list_t *l = (list_t*)malloc(sizeof(list_t));
31
32 l->size = 0;
33 l->head = 0;
34 l->tail = 0;
35
36 return l;
37 }
38 /*-------------------------------------------------------------------------*/
39 void list_free(list_t *l) {
40 list_clean(l);
41 free(l);
42 }
43 /*-------------------------------------------------------------------------*/
44 void list_clean(list_t *l) {
45 while (l->size)
46 list_rem_head(l);
47 }
48 /*-------------------------------------------------------------------------*/
49 void list_add_head(list_t *l, void *data) {
50 list_nd_t *nd = list_nd_alloc(data);
51 list_nd_t *old = l->head;
52
53 nd->prev = old;
54 l->head = nd;
55 l->size += 1;
56
57 if (old != 0)
58 old->next = nd;
59 else
60 l->tail = nd;
61 }
62 /*-------------------------------------------------------------------------*/
63 void list_add_tail(list_t *l, void *data) {
64 list_nd_t *nd = list_nd_alloc(data);
65 list_nd_t *old = l->tail;
66
67 nd->next = old;
68 l->tail = nd;
69 l->size += 1;
70
71 if (old != 0)
72 old->prev = nd;
73 else
74 l->head = nd;
75 }
76 /*-------------------------------------------------------------------------*/
77 void *list_rem_head(list_t *l) {
78 list_nd_t *nd = l->head;
79
80 if (nd == 0) return 0;
81
82 list_nd_t *old = nd->prev;
83
84 l->head = old;
85 l->size -= 1;
86
87 if (old != 0)
88 old->next = 0;
89 else
90 l->tail = 0;
91
92 return list_nd_free(nd);
93 }
94 /*-------------------------------------------------------------------------*/
95 void *list_rem_tail(list_t *l) {
96 list_nd_t *nd = l->tail;
97
98 if (nd == 0) return 0;
99
100 list_nd_t *old = nd->next;
101
102 l->tail = old;
103 l->size -= 1;
104
105 if (old != 0)
106 old->prev = 0;
107 else
108 l->head = 0;
109
110 return list_nd_free(nd);
111 }
112 /*-------------------------------------------------------------------------*/
113 list_nd_t *list_nd_alloc(void *data) {
114 list_nd_t *nd = (list_nd_t*)malloc(sizeof(list_nd_t));
115
116 nd->prev = 0;
117 nd->next = 0;
118 nd->data = data;
119
120 return nd;
121 }
122 /*-------------------------------------------------------------------------*/
123 void *list_nd_free(list_nd_t *nd) {
124 void *data = nd->data;
125
126 free(nd);
127
128 return data;
129 }