| Bsoft 2.0.5 Bernard's software package

linked_list.cpp
Go to the documentation of this file.
1
9 #include "utilities.h"
10
11 // Declaration of global variables
12 extern int verbose; // Level of output to the screen
13
26 char* add_item(char** list, unsigned long size)
27 {
28 char** curr_item = (char **) *list;
29 char* new_item = new char[size];
30 char* next_item = NULL;
31
32 memset(new_item, 0, size);
33
34 if ( verbose & VERB_DEBUG )
35 cout << "DEBUG add_item: " << &list << " " << &curr_item << " " << &new_item << endl;
36
37 if ( !curr_item )
38 *list = new_item;
39 else {
40 while ( ( next_item = *curr_item ) ) curr_item = (char **) next_item;
41 *curr_item = new_item;
42 }
43
44 return new_item;
45 }
46
59 char* append_item(char** list, char* item)
60 {
61 char** curr_item = (char **) *list;
62 char* next_item = NULL;
63
64 if ( verbose & VERB_DEBUG )
65 cout << "DEBUG append_item: " << &list << " " << &item << endl;
66
67 if ( !curr_item )
68 *list = item;
69 else {
70 while ( ( next_item = *curr_item ) ) curr_item = (char **) next_item;
71 *curr_item = item;
72 }
73
74 return item;
75 }
76
87 char* copy_item(char* toitem, char* fromitem, unsigned long size)
88 {
89 char* link = *((char **) toitem);
90
91 memcpy(toitem, fromitem, size);
92
93 memcpy(toitem, &link, sizeof(char*));
94
95 if ( verbose & VERB_DEBUG )
96 cout << "DEBUG copy_item: %" << &toitem << " " << &link << " " << &(*((char **) toitem)) << endl;
97
98 return toitem;
99 }
100
114 char* remove_item(char** list, char* item, unsigned long size)
115 {
116 if ( !(*list) || !item ) return NULL;
117
118 char** curr_item = (char **) *list; // First item pointer
119 char** next_item = (char **) item; // Item to be removed pointer
120
121 if ( verbose & VERB_DEBUG ) {
122 cout << "DEBUG remove_item: " << &list << " " << &curr_item << " " << &item << endl;
123 cout << "DEBUG remove_item: " << list << " " << curr_item << " " << item << endl;
124 }
125
126 if ( *list == item ) {
127 *list = *curr_item;
128 } else {
129 for ( ; curr_item && *curr_item != item; curr_item = (char **) *curr_item ) ;
130 *curr_item = *next_item;
131 }
132
133 delete[] item;
134
135 return *curr_item;
136 }
137
152 char* replace_item(char** list, char* item, char* new_item)
153 {
154 if ( !(*list) || !item ) return NULL;
155
156 char** curr_item = (char **) *list; // First item pointer
157 // char** next_item = (char **) item; // Item to be replaced pointer
158
159 if ( verbose & VERB_DEBUG ) {
160 cout << "DEBUG replace_item: " << &list << " " << &curr_item << " " << &item << " " << &new_item << endl;
161 cout << "DEBUG replace_item: " << list << " " << curr_item << " " << item << " " << new_item << endl;
162 }
163
164 if ( *list == item ) {
165 *list = new_item;
166 } else {
167 for ( ; curr_item && *curr_item != item; curr_item = (char **) *curr_item ) ;
168 *curr_item = new_item;
169 }
170
171 *new_item = *item;
172
173 delete[] item;
174 item = NULL;
175
176 return new_item;
177 }
178
185 char* copy_list(char* list, unsigned long size)
186 {
187 if ( !list ) return 0;
188
189 char** item;
190 char* newlist = NULL;
191 char* newitem = NULL;
192
193 for ( item = (char **) list; item; item = (char **) *item ) {
194 newitem = add_item(&newitem, size);
195 if ( !newlist ) newlist = newitem;
196 memcpy(newitem, item, size);
197 memset(newitem, 0, sizeof(char *));
198 }
199
200 return newlist;
201 }
202
203
214 long kill_list(char* list, unsigned long size)
215 {
216 if ( !list ) return 0;
217
218 long n(0);
219 char** item;
220
221 for ( item = (char **) list; item; n++ ) {
222 list = *item;
223 delete[] item;
224 item = (char **) list;
225 }
226
227 return n;
228 }
229
235 long count_list(char* list)
236 {
237 if ( !list ) return 0;
238
239 long n(0);
240 char** item;
241
242 for ( item = (char **) list; item; item = (char **) *item ) n++;
243
244 return n;
245 }
246
252 long reverse_list(char** list)
253 {
254 if ( !(*list) ) return 0;
255
256 if ( verbose & VERB_DEBUG )
257 cout << "DEBUG reverse_list: " << &list << endl;
258
259 long n(0);
260 char** item;
261 char** itemp = NULL;
262 char* link = NULL;
263
264 for ( n=0, item = (char **) *list; item; item = (char **) link, n++ ) {
265 link = *((char **) item);
266 if ( *list == (char *) item ) *item = NULL;
267 else *item = (char *) itemp;
268 itemp = item;
269 }
270
271 *list = (char *) itemp;
272
273 if ( verbose & VERB_DEBUG )
274 cout << "DEBUG reverse_list: " << n << " items reversed" << endl;
275
276 return n;
277 }
278
279
copy_list
char * copy_list(char *list, unsigned long size)
Generates a complete copy of a linked list.
Definition: linked_list.cpp:185
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
copy_item
char * copy_item(char *toitem, char *fromitem, unsigned long size)
Copies an item to an existing item without changing the link.
Definition: linked_list.cpp:87
verbose
int verbose
remove_item
char * remove_item(char **list, char *item, unsigned long size)
Finds the given item and deletes it from the linked list.
Definition: linked_list.cpp:114
append_item
char * append_item(char **list, char *item)
Appends an item to a linked list.
Definition: linked_list.cpp:59
reverse_list
long reverse_list(char **list)
Reverse the order of items in a linked list.
Definition: linked_list.cpp:252
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
replace_item
char * replace_item(char **list, char *item, char *new_item)
Finds the given item and replaces it with the new item.
Definition: linked_list.cpp:152