6 void list_add( list **list_ppt, void *val )
8 /* Martin A. Hansen, May 2008 */
10 /* Add a new singly linked list element with a pointer. */
12 list *list_pt = *list_ppt;
15 new = mem_get( sizeof( list ) );
24 void list_add_int( list_int **list_ppt, int val )
26 /* Martin A. Hansen, May 2008 */
28 /* Add a new singly linked list element with a integer. */
30 list_int *elem = NULL;
32 elem = mem_get( sizeof( list_int ) );
35 elem->next = *list_ppt;
40 void list_reverse( void *list_pt )
42 /* Martin A. Hansen, August 2008 */
44 /* Reverse the order of elements in a singly linked list. */
46 list **list_ppt = ( list ** ) list_pt;
54 while ( next != NULL )
66 bool list_exists( list *list_pt, char *string )
68 /* Martin A. Hansen, June 2008 */
70 /* Check if a given string exists in a singly linked list. */
74 elem = mem_get( sizeof( list ) );
76 for ( elem = list_pt; elem != NULL; elem = elem->next )
78 if ( strcmp( elem->val, string ) == 0 ) {
87 bool list_exists_int( list_int *list_pt, int val )
89 /* Martin A. Hansen, June 2008 */
91 /* Check if a given integer exists in a singly linked list. */
93 list_int *elem = NULL;
95 elem = mem_get( sizeof( list_int ) );
97 for ( elem = list_pt; elem != NULL; elem = elem->next )
99 if ( elem->val == val ) {
108 void list_free( void *list_pt )
110 /* Martin A. Hansen, June 2008 */
112 /* Free memory for all elements of a singly linked list. */
114 list **list_ppt = ( list ** ) list_pt;
115 list *next = *list_ppt;
118 while ( next != NULL )
122 // printf( "elem->val: %s\n", ( char * ) elem->val );
125 // mem_free( &elem );
133 void list_print( void *list_pt )
135 /* Martin A. Hansen, June 2008 */
137 /* Debug function to print all elements from a singly linked list. */
139 list *first = ( list * ) list_pt;
143 for ( elem = first; elem != NULL; elem = elem->next )
145 printf( "Elem %d: ->%s<-\n", i, ( char * ) elem->val );
152 void list_dl_new( list_dl **list_ppt )
154 /* Martin A. Hansen, August 2008 */
156 /* Initialize a new doubly linked list. */
160 new = mem_get( sizeof( list_dl ) );
169 void list_dl_add_beg( list_dl **list_ppt, node_dl **node_ppt )
171 /* Martin A. Hansen, August 2008 */
173 /* Add a new node to the beginning of a doubly linked list. */
175 list_dl *list_pt = *list_ppt;
176 node_dl *node_pt = *node_ppt;
178 if ( list_pt->first == NULL )
180 list_pt->first = node_pt;
181 list_pt->last = node_pt;
182 node_pt->next = NULL;
183 node_pt->prev = NULL;
187 list_dl_add_before( &list_pt, &list_pt->first, &node_pt );
192 void list_dl_add_end( list_dl **list_ppt, node_dl **node_ppt )
194 /* Martin A. Hansen, August 2008 */
196 /* Add a new node to the end of a doubly linked list. */
198 list_dl *list_pt = *list_ppt;
199 node_dl *node_pt = *node_ppt;
201 if ( list_pt->last == NULL ) {
202 list_dl_add_beg( &list_pt, &node_pt );
204 list_dl_add_after( &list_pt, &list_pt->last, &node_pt );
209 void list_dl_add_before( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
211 /* Martin A. Hansen, August 2008 */
213 /* Add a new node before a given node of a doubly linked list. */
215 list_dl *list_pt = *list_ppt;
216 node_dl *node_pt = *node_ppt;
217 node_dl *new_node_pt = *new_node_ppt;
219 new_node_pt->prev = node_pt->prev;
220 new_node_pt->next = node_pt;
222 if ( node_pt->prev == NULL ) {
223 list_pt->first = new_node_pt;
225 node_pt->prev->next = new_node_pt;
228 node_pt->prev = new_node_pt;
232 void list_dl_add_after( list_dl **list_ppt, node_dl **node_ppt, node_dl **new_node_ppt )
234 /* Martin A. Hansen, August 2008 */
236 /* Add a new node after a given node of a doubly linked list. */
238 list_dl *list_pt = *list_ppt;
239 node_dl *node_pt = *node_ppt;
240 node_dl *new_node_pt = *new_node_ppt;
242 new_node_pt->prev = node_pt;
243 new_node_pt->next = node_pt->next;
245 if ( node_pt->next == NULL ) {
246 list_pt->last = new_node_pt;
248 node_pt->next->prev = new_node_pt;
251 node_pt->next = new_node_pt;
255 void list_dl_remove( list_dl **list_ppt, node_dl **node_ppt )
257 /* Martin A. Hansen, August 2008 */
259 /* Remove a node from a doubly linked list. */
261 list_dl *list_pt = *list_ppt;
262 node_dl *node_pt = *node_ppt;
264 if ( node_pt->prev == NULL ) {
265 list_pt->first = node_pt->next;
267 node_pt->prev->next = node_pt->next;
270 if ( node_pt->next == NULL ) {
271 list_pt->last = node_pt->prev;
273 node_pt->next->prev = node_pt->prev;
276 mem_free( &node_pt );
282 void list_dl_print( list_dl *list_pt )
284 /* Martin A. Hansen, August 2008 */
286 /* Debug function to print all elements from a doubly linked list. */
288 node_dl *node = list_pt->first;
291 while ( node != NULL )
293 printf( "Node: %d val: %s\n", i, ( char * ) node->val );