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 void node_sl_destroy( node_sl **node_ppt )
203 /* Martin A. Hansen, September 2008 */
205 /* Free memory for singly linked list node and value. */
207 node_sl *node = *node_ppt;
209 mem_free( &node->val );
216 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DOUBLY LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
219 list_dl *list_dl_new()
221 /* Martin A. Hansen, August 2008 */
223 /* Initialize a new doubly linked list. */
225 list_dl *new_list = NULL;
227 new_list = mem_get( sizeof( list_dl ) );
229 new_list->first = NULL;
230 new_list->last = NULL;
236 node_dl *node_dl_new()
238 /* Martin A. Hansen, September 2008 */
240 /* Initialize a new doubly linked list node. */
242 node_dl *new_node = NULL;
244 new_node = mem_get( sizeof( node_dl ) );
246 new_node->next = NULL;
247 new_node->prev = NULL;
248 new_node->val = NULL;
254 void list_dl_add_beg( list_dl **list_ppt, node_dl **node_ppt )
256 /* Martin A. Hansen, August 2008 */
258 /* Add a new node to the beginning of a doubly linked list. */
260 list_dl *list_pt = *list_ppt;
261 node_dl *node_pt = *node_ppt;
263 if ( list_pt->first == NULL )
265 list_pt->first = node_pt;
266 list_pt->last = node_pt;
267 node_pt->next = NULL;
268 node_pt->prev = NULL;
272 list_dl_add_before( &list_pt, &list_pt->first, &node_pt );
277 void list_dl_add_end( list_dl **list_ppt, node_dl **node_ppt )
279 /* Martin A. Hansen, August 2008 */
281 /* Add a new node to the end of a doubly linked list. */
283 list_dl *list_pt = *list_ppt;
284 node_dl *node_pt = *node_ppt;
286 if ( list_pt->last == NULL ) {
287 list_dl_add_beg( &list_pt, &node_pt );
289 list_dl_add_after( &list_pt, &list_pt->last, &node_pt );
294 void list_dl_add_before( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
296 /* Martin A. Hansen, August 2008 */
298 /* Add a new node before a given node of a doubly linked list. */
300 list_dl *list_pt = *list_ppt;
301 node_dl *node_pt = *node_ppt;
302 node_dl *new_node_pt = *new_node_ppt;
304 new_node_pt->prev = node_pt->prev;
305 new_node_pt->next = node_pt;
307 if ( node_pt->prev == NULL ) {
308 list_pt->first = new_node_pt;
310 node_pt->prev->next = new_node_pt;
313 node_pt->prev = new_node_pt;
317 void list_dl_add_after( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
319 /* Martin A. Hansen, August 2008 */
321 /* Add a new node after a given node of a doubly linked list. */
323 list_dl *list_pt = *list_ppt;
324 node_dl *node_pt = *node_ppt;
325 node_dl *new_node_pt = *new_node_ppt;
327 new_node_pt->prev = node_pt;
328 new_node_pt->next = node_pt->next;
330 if ( node_pt->next == NULL ) {
331 list_pt->last = new_node_pt;
333 node_pt->next->prev = new_node_pt;
336 node_pt->next = new_node_pt;
340 void list_dl_remove( list_dl **list_ppt, node_dl **node_ppt )
342 /* Martin A. Hansen, August 2008 */
344 /* Remove a node from a doubly linked list. */
346 list_dl *list_pt = *list_ppt;
347 node_dl *node_pt = *node_ppt;
349 if ( node_pt->prev == NULL ) {
350 list_pt->first = node_pt->next;
352 node_pt->prev->next = node_pt->next;
355 if ( node_pt->next == NULL ) {
356 list_pt->last = node_pt->prev;
358 node_pt->next->prev = node_pt->prev;
361 mem_free( &node_pt );
367 void list_dl_print( list_dl *list_pt )
369 /* Martin A. Hansen, August 2008 */
371 /* Debug function to print all elements from a doubly linked list. */
373 node_dl *node = NULL;
375 for ( node = list_pt->first; node != NULL; node = node->next ) {
376 node_dl_print( node );
381 void node_dl_print( node_dl *node_pt )
383 /* Martin A. Hansen, September 2008 */
385 /* Debug funtion to print a doubly linked list node. */
387 printf( "node_dl->val: %s\n", ( char * ) node_pt->val );
391 void list_dl_destroy( list_dl **list_ppt )
393 /* Martin A. Hansen, August 2008 */
395 /* Free memory for all nodes in and including the doubly linked list. */
397 list_dl *list_pt = *list_ppt;
398 node_dl *next = list_pt->first;
399 node_dl *node = NULL;
401 while ( next != NULL )
409 mem_free( &list_pt );
415 void node_dl_destroy( node_dl **node_ppt )
417 /* Martin A. Hansen, September 2008 */
419 /* Free memory for doubly linked list node and value. */
421 node_dl *node = *node_ppt;
423 mem_free( &node->val );
430 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GENERIC LINKED LIST <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
433 size_t list_count( void *list_pt )
435 /* Martin A. Hansen, September 2008 */
437 /* Returns the number of nodes in a linked list. */
439 list_sl *list = ( list_sl * ) list_pt;
440 node_sl *node = NULL;
443 for ( node = list->first; node != NULL; node = node->next ) {
451 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ASSORTED SORTING FUNCTIOS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
454 int cmp_list_sl_char_asc( const void *a, const void *b )
456 /* Martin A. Hansen, September 2008 */
458 /* Compare function for use with list_sl_sort(). */
459 /* Sort in ascending order according to char node values. */
461 node_sl *a_node = *( ( node_sl ** ) a );
462 node_sl *b_node = *( ( node_sl ** ) b );
464 return strcmp( ( char * ) a_node->val, ( char * ) b_node->val );
468 int cmp_list_sl_char_desc( const void *a, const void *b )
470 /* Martin A. Hansen, September 2008 */
472 /* Compare function for use with list_sl_sort(). */
473 /* Sort in descending order according to char node values. */
475 node_sl *a_node = *( ( node_sl ** ) a );
476 node_sl *b_node = *( ( node_sl ** ) b );
478 return strcmp( ( char * ) b_node->val, ( char * ) a_node->val );
482 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/