]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/list.c
added remove_adaptor biopiece
[biopieces.git] / code_c / Maasha / src / lib / list.c
1 #include "common.h"
2 #include "mem.h"
3 #include "list.h"
4
5
6 void list_add( list **list_ppt, void *val )
7 {
8     /* Martin A. Hansen, May 2008 */
9
10     /* Add a new singly linked list element with a pointer. */
11
12     list *list_pt = *list_ppt;
13     list *new     = NULL;
14
15     new = mem_get( sizeof( list ) );
16
17     new->val  = val;
18     new->next = list_pt;
19
20     *list_ppt = new;
21 }
22
23
24 void list_add_int( list_int **list_ppt, int val )
25 {
26     /* Martin A. Hansen, May 2008 */
27
28     /* Add a new singly linked list element with a integer. */
29
30     list_int *elem = NULL;
31
32     elem = mem_get( sizeof( list_int ) );
33
34     elem->val  =  val;
35     elem->next = *list_ppt;
36     *list_ppt  =  elem;
37 }
38
39
40 void list_reverse( void *list_pt )
41 {
42     /* Martin A. Hansen, August 2008 */
43
44     /* Reverse the order of elements in a singly linked list. */
45
46     list **list_ppt = ( list ** ) list_pt;
47     list *pt        = *list_ppt;
48     list *new       = NULL;
49     list *next      = NULL;
50     list *temp      = NULL;
51
52     next = pt;
53
54     while ( next != NULL )
55     {
56         temp       = next;
57         next       = temp->next;
58         temp->next = new;
59         new        = temp;
60     }
61
62     *list_ppt = new;
63 }
64
65
66 bool list_exists( list *list_pt, char *string )
67 {
68     /* Martin A. Hansen, June 2008 */
69
70     /* Check if a given string exists in a singly linked list. */
71
72     list *elem = NULL;
73
74     elem = mem_get( sizeof( list ) );
75
76     for ( elem = list_pt; elem != NULL; elem = elem->next )
77     {
78         if ( strcmp( elem->val, string ) == 0 ) {
79             return TRUE;                                                                                                        
80         }                                                                                                                       
81     }                                                                                                                           
82
83     return FALSE;                                                                                                               
84 }                                                                                                                               
85
86
87 bool list_exists_int( list_int *list_pt, int val )
88 {
89     /* Martin A. Hansen, June 2008 */
90
91     /* Check if a given integer exists in a singly linked list. */
92
93     list_int *elem = NULL;
94
95     elem = mem_get( sizeof( list_int ) );
96
97     for ( elem = list_pt; elem != NULL; elem = elem->next )
98     {
99         if ( elem->val == val ) {
100             return TRUE;                                                                                                        
101         }                                                                                                                       
102     }                                                                                                                           
103
104     return FALSE;                                                                                                               
105 }                                                                                                                               
106
107
108 void list_free( void *list_pt )
109 {
110     /* Martin A. Hansen, June 2008 */
111
112     /* Free memory for all elements of a singly linked list. */
113
114     list **list_ppt = ( list ** ) list_pt;
115     list  *next     = *list_ppt;
116     list  *elem     = NULL;
117
118     while ( next != NULL )
119     {
120         elem = next;
121
122 //        printf( "elem->val: %s\n", ( char * ) elem->val );
123
124         next = elem->next;
125 //        mem_free( &elem );
126         free( elem );
127     }
128
129     *list_ppt = NULL;
130 }
131
132
133 void list_print( void *list_pt )
134 {
135     /* Martin A. Hansen, June 2008 */
136     
137     /* Debug function to print all elements from a singly linked list. */
138     
139     list *first = ( list * ) list_pt;
140     list *elem  = NULL;
141     int   i     = 0;
142     
143     for ( elem = first; elem != NULL; elem = elem->next )
144     {
145         printf( "Elem %d: ->%s<-\n", i, ( char * ) elem->val );
146         
147         i++;
148     }   
149 }   
150
151
152 void list_dl_new( list_dl **list_ppt )
153 {
154     /* Martin A. Hansen, August 2008 */
155
156     /* Initialize a new doubly linked list. */
157
158     list_dl *new = NULL;
159
160     new = mem_get( sizeof( list_dl ) );
161
162     new->first = NULL;
163     new->last  = NULL;
164
165     *list_ppt = new;
166 }
167
168
169 void list_dl_add_beg( list_dl **list_ppt, node_dl **node_ppt )
170 {
171     /* Martin A. Hansen, August 2008 */
172
173     /* Add a new node to the beginning of a doubly linked list. */
174
175     list_dl *list_pt = *list_ppt;
176     node_dl *node_pt = *node_ppt;
177
178     if ( list_pt->first == NULL )
179     {
180         list_pt->first = node_pt;
181         list_pt->last  = node_pt;
182         node_pt->next  = NULL;
183         node_pt->prev  = NULL;
184     }
185     else
186     {
187         list_dl_add_before( &list_pt, &list_pt->first, &node_pt );
188     }
189 }
190
191
192 void list_dl_add_end( list_dl **list_ppt, node_dl **node_ppt )
193 {
194     /* Martin A. Hansen, August 2008 */
195
196     /* Add a new node to the end of a doubly linked list. */
197
198     list_dl *list_pt = *list_ppt;
199     node_dl *node_pt = *node_ppt;
200
201     if ( list_pt->last == NULL ) {
202         list_dl_add_beg( &list_pt, &node_pt );
203     } else {
204         list_dl_add_after( &list_pt, &list_pt->last, &node_pt );
205     }
206 }
207
208
209 void list_dl_add_before( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
210 {
211     /* Martin A. Hansen, August 2008 */
212
213     /* Add a new node before a given node of a doubly linked list. */
214
215     list_dl *list_pt     = *list_ppt;
216     node_dl *node_pt     = *node_ppt;
217     node_dl *new_node_pt = *new_node_ppt;
218
219     new_node_pt->prev = node_pt->prev;
220     new_node_pt->next = node_pt;
221
222     if ( node_pt->prev == NULL ) {
223         list_pt->first = new_node_pt;
224     } else {
225         node_pt->prev->next = new_node_pt;
226     }
227
228     node_pt->prev = new_node_pt;
229 }
230
231
232 void list_dl_add_after( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
233 {
234     /* Martin A. Hansen, August 2008 */
235
236     /* Add a new node after a given node of a doubly linked list. */
237
238     list_dl *list_pt     = *list_ppt;
239     node_dl *node_pt     = *node_ppt;
240     node_dl *new_node_pt = *new_node_ppt;
241
242     new_node_pt->prev = node_pt;
243     new_node_pt->next = node_pt->next;
244
245     if ( node_pt->next == NULL ) {
246         list_pt->last = new_node_pt;
247     } else {
248         node_pt->next->prev = new_node_pt;
249     }
250
251     node_pt->next = new_node_pt;
252 }
253
254
255 void list_dl_remove( list_dl **list_ppt, node_dl **node_ppt )
256 {
257     /* Martin A. Hansen, August 2008 */
258
259     /* Remove a node from a doubly linked list. */
260
261     list_dl *list_pt = *list_ppt;
262     node_dl *node_pt = *node_ppt;
263
264     if ( node_pt->prev == NULL ) {
265         list_pt->first = node_pt->next;
266     } else {
267         node_pt->prev->next = node_pt->next;
268     }
269
270     if ( node_pt->next == NULL ) {
271         list_pt->last = node_pt->prev;
272     } else {
273         node_pt->next->prev = node_pt->prev;
274     }
275
276     mem_free( &node_pt );
277
278 //    *node_ppt = NULL;
279 }
280
281
282 void list_dl_print( list_dl *list_pt )
283 {
284     /* Martin A. Hansen, August 2008 */
285
286     /* Debug function to print all elements from a doubly linked list. */
287
288     node_dl *node = list_pt->first;
289     int      i    = 0;
290
291     while ( node != NULL )
292     {
293         printf( "Node: %d val: %s\n", i, ( char * ) node->val );
294
295         node = node->next;
296
297         i++;
298     }
299 }