1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
8 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SINGLY LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
11 list_sl *list_sl_new()
13 /* Martin A. Hansen, August 2008 */
15 /* Initialize a new singly linked list. */
17 list_sl *new_list = NULL;
19 new_list = mem_get( sizeof( list_sl ) );
21 new_list->first = NULL;
27 node_sl *node_sl_new()
29 /* Martin A. Hansen, September 2008 */
31 /* Initialize a new singly linked list node. */
33 node_sl *new_node = NULL;
35 new_node = mem_get( sizeof( node_sl ) );
37 new_node->next = NULL;
44 void list_sl_add_beg( list_sl **list_ppt, node_sl **node_ppt )
46 /* Martin A. Hansen, August 2008 */
48 /* Add a new node to the beginning of a singly linked list. */
50 list_sl *list_pt = *list_ppt;
51 node_sl *node_pt = *node_ppt;
53 node_pt->next = list_pt->first;
54 list_pt->first = node_pt;
58 void list_sl_add_after( node_sl **node_ppt, node_sl **new_node_ppt )
60 /* Martin A. Hansen, August 2008 */
62 /* Add a new node after a given node of a singly linked list. */
64 node_sl *node_pt = *node_ppt;
65 node_sl *new_node_pt = *new_node_ppt;
67 new_node_pt->next = node_pt->next;
68 node_pt->next = new_node_pt;
72 void list_sl_remove_beg( list_sl **list_ppt )
74 /* Martin A. Hansen, August 2008 */
76 /* Remove the first node of a singly linked list. */
78 list_sl *list_pt = *list_ppt;
79 node_sl *old_node = NULL;
81 old_node = list_pt->first;
82 list_pt->first = list_pt->first->next;
84 mem_free( &old_node );
88 void list_sl_remove_after( node_sl **node_ppt )
90 /* Martin A. Hansen, August 2008 */
92 /* Remove the node next to this one in a singly linked list. */
94 node_sl *node_pt = *node_ppt;
95 node_sl *old_node = NULL;
97 old_node = node_pt->next;
98 node_pt->next = node_pt->next->next;
100 mem_free( &old_node );
104 void list_sl_print( list_sl *list_pt )
106 /* Martin A. Hansen, August 2008 */
108 /* Debug function to print all elements from a singly linked list. */
110 node_sl *node = NULL;
112 for ( node = list_pt->first; node != NULL; node = node->next ) {
113 node_sl_print( node );
118 void node_sl_print( node_sl *node_pt )
120 /* Martin A. Hansen, September 2008 */
122 /* Debug funtion to print a singly linked list node. */
124 printf( "node_sl->val: %s\n", ( char * ) node_pt->val );
128 void list_sl_sort( list_sl **list_ppt, int ( *compare )( const void *a, const void *b ) )
130 /* Martin A. Hansen, September 2008 */
132 /* Sort a singly linked list according to the compare function. */
134 list_sl *list = *list_ppt;
135 node_sl **node_array = NULL; /* array of pointers to nodes */
136 node_sl *node = NULL;
137 node_sl *old_node = NULL;
141 count = list_count( list );
145 node_array = mem_get( count * sizeof( *node_array ) );
147 for ( node = list->first, i = 0; node != NULL; node = node->next, i++ ) {
148 node_array[ i ] = node;
151 qsort( node_array, count, sizeof( node_array[ 0 ] ), compare );
155 node = node_array[ 0 ];
157 list_sl_add_beg( &list, &node );
161 for ( i = 1; i < count; i++ )
163 node = node_array[ i ];
165 list_sl_add_after( &old_node, &node );
172 mem_free( &node_array );
177 void list_sl_destroy( list_sl **list_ppt )
179 /* Martin A. Hansen, August 2008 */
181 /* Free memory for all nodes in and including the singly linked list. */
183 list_sl *list_pt = *list_ppt;
184 node_sl *next = list_pt->first;
185 node_sl *node = NULL;
187 while ( next != NULL )
195 mem_free( &list_pt );
201 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DOUBLY LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
204 list_dl *list_dl_new()
206 /* Martin A. Hansen, August 2008 */
208 /* Initialize a new doubly linked list. */
210 list_dl *new_list = NULL;
212 new_list = mem_get( sizeof( list_dl ) );
214 new_list->first = NULL;
215 new_list->last = NULL;
221 node_dl *node_dl_new()
223 /* Martin A. Hansen, September 2008 */
225 /* Initialize a new doubly linked list node. */
227 node_dl *new_node = NULL;
229 new_node = mem_get( sizeof( node_dl ) );
231 new_node->next = NULL;
232 new_node->prev = NULL;
233 new_node->val = NULL;
239 void list_dl_add_beg( list_dl **list_ppt, node_dl **node_ppt )
241 /* Martin A. Hansen, August 2008 */
243 /* Add a new node to the beginning of a doubly linked list. */
245 list_dl *list_pt = *list_ppt;
246 node_dl *node_pt = *node_ppt;
248 if ( list_pt->first == NULL )
250 list_pt->first = node_pt;
251 list_pt->last = node_pt;
252 node_pt->next = NULL;
253 node_pt->prev = NULL;
257 list_dl_add_before( &list_pt, &list_pt->first, &node_pt );
262 void list_dl_add_end( list_dl **list_ppt, node_dl **node_ppt )
264 /* Martin A. Hansen, August 2008 */
266 /* Add a new node to the end of a doubly linked list. */
268 list_dl *list_pt = *list_ppt;
269 node_dl *node_pt = *node_ppt;
271 if ( list_pt->last == NULL ) {
272 list_dl_add_beg( &list_pt, &node_pt );
274 list_dl_add_after( &list_pt, &list_pt->last, &node_pt );
279 void list_dl_add_before( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
281 /* Martin A. Hansen, August 2008 */
283 /* Add a new node before a given node of a doubly linked list. */
285 list_dl *list_pt = *list_ppt;
286 node_dl *node_pt = *node_ppt;
287 node_dl *new_node_pt = *new_node_ppt;
289 new_node_pt->prev = node_pt->prev;
290 new_node_pt->next = node_pt;
292 if ( node_pt->prev == NULL ) {
293 list_pt->first = new_node_pt;
295 node_pt->prev->next = new_node_pt;
298 node_pt->prev = new_node_pt;
302 void list_dl_add_after( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
304 /* Martin A. Hansen, August 2008 */
306 /* Add a new node after a given node of a doubly linked list. */
308 list_dl *list_pt = *list_ppt;
309 node_dl *node_pt = *node_ppt;
310 node_dl *new_node_pt = *new_node_ppt;
312 new_node_pt->prev = node_pt;
313 new_node_pt->next = node_pt->next;
315 if ( node_pt->next == NULL ) {
316 list_pt->last = new_node_pt;
318 node_pt->next->prev = new_node_pt;
321 node_pt->next = new_node_pt;
325 void list_dl_remove( list_dl **list_ppt, node_dl **node_ppt )
327 /* Martin A. Hansen, August 2008 */
329 /* Remove a node from a doubly linked list. */
331 list_dl *list_pt = *list_ppt;
332 node_dl *node_pt = *node_ppt;
334 if ( node_pt->prev == NULL ) {
335 list_pt->first = node_pt->next;
337 node_pt->prev->next = node_pt->next;
340 if ( node_pt->next == NULL ) {
341 list_pt->last = node_pt->prev;
343 node_pt->next->prev = node_pt->prev;
346 mem_free( &node_pt );
352 void list_dl_print( list_dl *list_pt )
354 /* Martin A. Hansen, August 2008 */
356 /* Debug function to print all elements from a doubly linked list. */
358 node_dl *node = NULL;
360 for ( node = list_pt->first; node != NULL; node = node->next ) {
361 node_dl_print( node );
366 void node_dl_print( node_dl *node_pt )
368 /* Martin A. Hansen, September 2008 */
370 /* Debug funtion to print a doubly linked list node. */
372 printf( "node_dl->val: %s\n", ( char * ) node_pt->val );
376 void list_dl_destroy( list_dl **list_ppt )
378 /* Martin A. Hansen, August 2008 */
380 /* Free memory for all nodes in and including the doubly linked list. */
382 list_dl *list_pt = *list_ppt;
383 node_dl *next = list_pt->first;
384 node_dl *node = NULL;
386 while ( next != NULL )
394 mem_free( &list_pt );
400 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GENERIC LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
403 size_t list_count( void *list_pt )
405 /* Martin A. Hansen, September 2008 */
407 /* Returns the number of nodes in a linked list. */
409 list_sl *list = ( list_sl * ) list_pt;
410 node_sl *node = NULL;
413 for ( node = list->first; node != NULL; node = node->next ) {
421 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ASSORTED SORTING FUNCTIOS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
424 int cmp_list_sl_char_asc( const void *a, const void *b )
426 /* Martin A. Hansen, September 2008 */
428 /* Compare function for use with list_sl_sort(). */
429 /* Sort in ascending order according to char node values. */
431 node_sl *a_node = *( ( node_sl ** ) a );
432 node_sl *b_node = *( ( node_sl ** ) b );
434 return strcmp( ( char * ) a_node->val, ( char * ) b_node->val );
438 int cmp_list_sl_char_desc( const void *a, const void *b )
440 /* Martin A. Hansen, September 2008 */
442 /* Compare function for use with list_sl_sort(). */
443 /* Sort in descending order according to char node values. */
445 node_sl *a_node = *( ( node_sl ** ) a );
446 node_sl *b_node = *( ( node_sl ** ) b );
448 return strcmp( ( char * ) b_node->val, ( char * ) a_node->val );
452 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/