]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/list.c
unit test of mem.c done
[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( struct 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     struct list *elem = NULL;
13
14     elem = mem_get( sizeof( elem ) );
15
16     elem->val     = val;
17     elem->next    = *( list_ppt );
18     *( list_ppt ) = ( elem );
19 }
20
21
22 void list_add_int( struct list_int **list_ppt, int val )
23 {
24     /* Martin A. Hansen, May 2008 */
25
26     /* Add a new singly linked list element with a integer. */
27
28     struct list_int *elem = NULL;
29
30     elem = mem_get( sizeof( elem ) );
31
32     elem->val     = val;
33 //    elem->next    = *( list_ppt );
34     *( list_ppt ) = ( elem );
35 }
36
37
38 void list_reverse( void *old_list )
39 {
40     /* Martin A. Hansen, May 2008 */
41
42     /* Reverse the order of elements in a singly linked list. */
43
44     struct list **ppt = ( struct list ** ) old_list;
45     struct list *new_list = NULL;
46     struct list *elem;
47     struct list *next;
48
49     next = *ppt;
50
51     while ( next != NULL )
52     {
53         elem       = next;
54         next       = elem->next;
55         elem->next = new_list;
56         new_list   = elem;
57     }
58
59     *ppt = new_list;
60 }
61
62
63 bool list_exists( struct list *list_pt, char *string )
64 {
65     /* Martin A. Hansen, June 2008 */
66
67     /* Check if a given string exists in a singly linked list. */
68
69     struct list *elem;
70
71     elem = mem_get( sizeof( elem ) );
72
73     for ( elem = list_pt; elem != NULL; elem = elem->next )
74     {
75         if ( strcmp( elem->val, string ) == 0 ) {
76             return TRUE;                                                                                                        
77         }                                                                                                                       
78     }                                                                                                                           
79
80     return FALSE;                                                                                                               
81 }                                                                                                                               
82
83
84 bool list_exists_int( struct list_int *list_pt, int val )
85 {
86     /* Martin A. Hansen, June 2008 */
87
88     /* Check if a given integer exists in a singly linked list. */
89
90     struct list_int *elem;
91
92     elem = mem_get( sizeof( elem ) );
93
94 //    for ( elem = list_pt; elem != NULL; elem = elem->next )
95     {
96         if ( elem->val == val ) {
97             return TRUE;                                                                                                        
98         }                                                                                                                       
99     }                                                                                                                           
100
101     return FALSE;                                                                                                               
102 }                                                                                                                               
103
104
105 void list_free( void *list_pt )
106 {
107     /* Martin A. Hansen, June 2008 */
108
109     /* Free memory for all elements of a singly linked list. */
110
111     struct list **ppt  = ( struct list ** ) list_pt;
112     struct list  *next = *ppt;
113     struct list  *elem;
114
115     while ( next != NULL )
116     {
117         elem = next;
118         next = elem->next;
119         mem_free( ( void * ) &elem );
120     }
121
122     ppt = NULL;
123 }
124
125
126 void list_print( struct list *list_pt )
127 {
128     /* Martin A. Hansen, June 2008 */
129     
130     /* Debug function to print all elements from a singly linked list. */
131     
132     int i = 0;
133     
134     struct list *elem;
135     
136     for ( elem = list_pt; elem != NULL; elem = elem->next )
137     {
138         printf( "elem %d: ->%s<-\n", i, ( char * ) elem->val );
139         
140         i++;
141     }   
142 }   
143