0628c38e469659bbefa57bae9bc7a4c2

Hello, im not sure whether to construct the list using a function pointer to the "delete" function of the object. Or even if i must create a "list_item_t*" for the list links and data, and keep the list_t* only for common list information (such as the eventual "data deleter" function pointer).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_
#include "common.h"

typedef struct _s_list list_t;
/* TODO: add comments */

/* Description: Creates a list, this function sets the list to empty,
 * Arguments:
 * 		no arguments
 * Return: Returns a pointer to the empty list, the list has type 'list_t'
 * Errors: On error, NULL is returned, errno is set accordingly and an error
 * is printed to stderr
 * Depends:
 * void *malloc(size_t size); <stdlib.h>
 */
list_t* DLL_EXPORT list_create ( void );

/* Description: Deletes a list, this function frees all of the list occupied
 * 		memory
 * Arguments:
 * 		list_t* _list: a pointer to the list to be deleted
 * Return: returns no value
 * Depends:
 * void free(void *ptr); <stdlib.h>
 */
void DLL_EXPORT list_delete ( list_t* _list );

/* Description: Gets the first element of a list, the element is not removed
 * 		from the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the first element
 * 		is to be fetched
 * Return: Returns a pointer to the first element of the list, the list
 * 	   element has type list_t*
 * Errors: On error, NULL is returned and the error is printed to stderr
 * Depends:
 */

list_t* DLL_EXPORT list_gethead ( list_t *const _list ) __ATTR_CONST;

/* Description: Gets the elements that preceeds the first element of the list,
 * 		the first element is not removed from the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the elements are to
 * 		be fetched
 * Return: Returns a pointer to the next element of the first element of the
 * 	   list, this element has type list_t*
 * Errors: On error, NULL is returned and the error is printed to stderr
 * Depends:
 */
list_t* DLL_EXPORT list_gettail ( const list_t *const _list ) __ATTR_CONST;

/* Description: Gets the last element of the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the last element is
 * 		to be fetched
 * Return: Returns a pointer to the last element of the list, this element has
 * 	   type list_t*
 * Errors: On error, NULL is returned and the error is printed to stderr
 * Depends:
 */
list_t* DLL_EXPORT list_getend ( list_t *const _list ) __ATTR_CONST;

/* Description: Inserts an element to the end of the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the element is to be  *		inserted
 * 		void* _elem: a pointer to the element to insert
 * Return: Returns a pointer to the last element of the list, this element has
 * 	   type list_t*
 * Errors: On error, NULL is returned, errno is set accordingly, and the error
 * 	   is printed to stderr
 * Depends:
 * void *malloc(size_t size); <stdlib.h>
 * list_t* list_get_end(list_t* _list); "linked_list.h"
 */
list_t* DLL_EXPORT list_appendelement ( list_t*const _list, void*const _elem );

/* Description: Inserts an element to the start of the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the element is to be  *		inserted
 * 		void* _elem: a pointer to the element to insert
 * Return: Returns a pointer to the first element of the list, this element has
 * 	   type list_t*
 * Errors: On error, NULL is returned, errno is set accordingly, and the error
 * 	   is printed to stderr
 * Depends:
 * void *malloc(size_t size); <stdlib.h>
 * list_t* list_get_head(list_t* _list); "linked_list.h"
 */
list_t* DLL_EXPORT list_insertelement ( list_t*const _list, void* _elem );


/* Description: Deletes an element from the list
 * Arguments:
 * 		list_t* _list: a pointer to the list where the element is to be  *		deleted
 * 		list_t* _elem: a pointer to the element to delete
 * Return: Returns no value
 * Errors: On error, NULL is returned, errno is set accordingly, and the error
 * 	   is printed to stderr
 * Depends:
 * void free(void *ptr); <stdlib.h>
 * list_t* list_get_tail(list_t* _list); "linked_list.h"
 */
void DLL_EXPORT list_deleteelement ( list_t* _list, list_t* _elem );

/* Description: Gets the data from the list element
 * Arguments:
 * 		list_t* _elem: a pointer to the list position where the data
 * 		is going to be read
 * Return: Returns a pointer to the data, this pointer has type void*
 */
void* DLL_EXPORT list_getdata ( list_t const* _elem) __ATTR_CONST;

/* Description: Determines if the list has at least one more element
 * Arguments:
 * 		list_t* _elem: a pointer to the list
 * Return: Returns true if the list has at least one more element, false
 * otherwise
 */
GLboolean DLL_EXPORT list_hasnext ( list_t const* _list ) __ATTR_CONST;

/* Description: Searches a list for a given element, a comparator is specified
 * 		as an argument that is used to find the given element
 * Arguments:
 * 		list_t* _list: a pointer to the list
 *		bool (*_compare)( list_t* _list_elem ): a pointer to the
 *		comparator function
 * Return: Returns the given element if it is found, NULL otherwise
 * Depends:
 * list_t* list_get_tail(list_t* _list); "linked_list.h"
 * list_t* list_get_head(list_t* _list); "linked_list.h"
 */
list_t* DLL_EXPORT list_findelement ( list_t *const _list,
				bool (*_compare)( list_t* _list_elem ) );

/* Description: Simple print function for lists, only prints lists that are
 * 		made of elementary data types
 * Arguments:
 * 		list_t* _list: a pointer to the list to be printed
 *		type_t _data_type: elementary data type of the list elements
 * Return: Returns no value
 * Depends:
 */
void list_print ( list_t *_list, type_t _data_type );
#endif


// FILE linked_list.c
#include "data_structures/linked_list.h"
struct _s_list
{
	void* data;	/* the data this list will hold */

	/* double linked */
	struct list* next;
	struct list* previous;
};

/* PRIVATE FUNCTIONS */
static GLboolean list_ishead ( const list_t *const _list ) __ATTR_CONST;
static void list_initializehead ( list_t *const _list );
static GLboolean list_isempty ( const list_t *const _list ) __ATTR_CONST;
static list_t* list_movetofront ( list_t* _list, list_t* _elem );

static GLboolean list_ishead ( const list_t *const _list )
{
	if ( _list->previous == NULL )
	{
		return GL_TRUE;
	}
	return GL_FALSE;

}

static void list_initializehead ( list_t *const _list )
{
	/* low priority error checking */
	if ( unlikely ( _list == NULL ) )
	{
		perror("list_initializehead()");
		return;
	}
	/* end of error checking */
	_list->data = NULL;
	_list->next = NULL;
	_list->previous = NULL;
}

static GLboolean list_isempty ( const list_t *const _list )
{
	/* low priority error checking */
	if ( unlikely ( _list == NULL ) )
	{
		perror("list_isempty()");
	}
	/* end of error checking */

	return (!_list->data && !_list->next && !_list->previous);
}

static list_t* list_movetofront ( list_t* _list, list_t* _elem )
{
	list_t* list_head;
	list_t* list_tail;
	list_t* previous_of_elem;
	/* low priority error checking */
	if ( unlikely ( _list == NULL || _elem == NULL) )
	{

	}
	/* end of error checking */

	list_head = list_gethead ( _list );
	list_tail = list_gettail ( _list );
	previous_of_elem = _elem->previous;
	if ( _elem->next == NULL ) /* _elem is at the end of the list */
	{
		if ( _elem->previous == NULL )
		{
			return _list; /* list has only one element (which is
					 the one we are trying to move into the
					 front of the list) */
		}
		previous_of_elem->next = NULL;
	}
	else if ( _elem->previous != NULL ) /* _elem is on the middle */
	{
		list_t* next_of_elem = _elem->next;
		next_of_elem->previous = _elem->previous;
	}
	else if ( _elem->previous == NULL )
	{
		return _list; /* the element we are trying to move into the
				 front of the list is already there */
	}
	previous_of_elem->next = _elem->next;
	list_head->previous = _elem;
	_elem->next = list_head;
	_elem->previous = NULL;

	return _elem;
}


/* PUBLIC FUNCTIONS */
list_t* list_create ( void )
{
	list_t* list;
	list = (list_t*) malloc ( sizeof ( list_t ) );
	/* low priority error checking */
	if ( unlikely ( list == NULL ) )
	{
		fprintf(stderr, "list_create() malloc()");
		return NULL;
	}
	/* end of error checking */

	list_initializehead ( list );

	return list;
}

void list_delete ( list_t* _list )
{
	/* high priority error checking */
	if ( !list_ishead ( _list ) )
	{
		/* begin traversing list backwise */
		list_t* tmp_list = _list->previous;
		do
		{
			list_t* last_pointer = tmp_list;
			free ( tmp_list->data );
			tmp_list = tmp_list->previous;
			free ( last_pointer );

		} while ( tmp_list != NULL );

	}
	/* end of error checking */

	while ( _list != NULL )
	{
		list_t* last_pointer = _list; /* keep a copy to delete after
					         advancing the pointer */
		free ( _list->data );
		_list = _list->next; 	      /* advance the pointer */
		free ( last_pointer ); 	      /* and delete the last one */
	}
}

list_t* list_gethead ( list_t*const _list )
{
	/* medium periority error checking */
	if ( unlikely ( _list == NULL ) )
	{
		perror ( "list_gethead ()" );
		return NULL;
	}
	/* end of error checking */

	if ( unlikely (list_ishead ( _list ) ) )
		return _list;
	return list_gethead ( _list->previous );
}

list_t* list_gettail ( const list_t *const _list )
{
	/* medium priority error checking */
	if ( unlikely ( _list == NULL ) )
	{
		perror ( "list_gettail()" );
		return NULL;
	}
	if ( unlikely ( _list->next == NULL ) )
	{
		return NULL;
	}
	/* end of error checking */

	return _list->next;
}

list_t* list_getend ( list_t *const _list )
{
	/* medium priority error checking */
	if ( unlikely( _list == NULL ) )
	{
		fprintf(stderr, "list_getend()" );
		return NULL;
	}
	/* end of error checking */
	if ( _list->next == NULL ) return _list;
	return list_getend ( _list->next );
}

list_t* list_insertelement ( list_t*const _list, void* _elem )
{
	list_t* list_head;
	list_t* new_element;

	/* medium priority error checking */
	if ( _list == NULL || _elem == NULL )
	{
		errno = EINVAL;
		return NULL;
	}
	/* end of error checking */
	list_head = list_gethead ( _list );

	if ( !list_isempty ( _list ) ) /* list is not empty */
	{
		new_element = (list_t*) malloc( sizeof ( list_t ) );
		/* low priority error checking */
		if( unlikely ( new_element == NULL ) )
		{
			perror ( "list_insertelement() malloc()" );
		}
		/* end of error checking */

		/* update pointers */
		list_head->previous = new_element;
	}
	else /* list is empty */
	{
		list_head = NULL;
		new_element = _list;
	}
	new_element->next = list_head;
	new_element->previous = NULL;

	/* add our data */
	new_element->data = _elem;

	return new_element;
}

list_t* list_appendelement ( list_t*const _list, void*const _elem )
{
	list_t* list_end;
	list_t* new_element;

	/* medium priority error checking */
	if ( unlikely(_list == NULL || _elem == NULL ))
	{
		errno = EINVAL;
		return NULL;
	}
	/* end of error checking */
	list_end = list_getend ( _list );

	if ( !list_isempty ( _list ) ) /* list is not empty */
	{
		new_element = (list_t*) malloc( sizeof ( list_t ) );
		/* low priority error checking */
		if( unlikely ( new_element == NULL ) )
		{
			perror ( "list_insertelement() malloc()" );
			return NULL;
		}
		/* end of error checking */

		/* update pointers */
		list_end->next = new_element;
	}
	else /* list is empty */
	{
		list_end = NULL;
		new_element = _list;
	}
	new_element->next = NULL;
	new_element->previous = list_end;

	/* add our data */
	new_element->data = _elem;

	return new_element;
}

void* list_getdata ( list_t const* _list )
{
	return _list->data;
}

GLboolean list_hasnext ( list_t const* _list )
{
	if ( _list != NULL && _list->next == NULL ) return GL_FALSE;
	return GL_TRUE;
}

void list_deleteelement ( list_t* _list, list_t* _elem )
{
	list_t* new_list;
	list_t* final_list;
	/* medium priority error checking */
	if ( _list == NULL || _elem == NULL )
	{
		return;
	}
	/* end of error checking */

	new_list = list_movetofront ( _list, _elem );
	final_list = list_gettail ( new_list );
	final_list->previous = NULL;
	free ( new_list->data );
	free ( new_list );
}

list_t* list_findelement ( list_t *const _list,
				GLboolean (*_compare)( list_t* _list_elem ) )
{
	list_t* list_iterator;

	list_iterator = list_gethead ( _list );
	do
	{
		if (unlikely (_compare (list_iterator)))
		{
			return list_iterator;
		}
		list_iterator = list_gettail ( list_iterator );
	} while ( list_iterator != NULL );
	return NULL;
}

void list_print ( list_t* _list, type_t _data_type )
{
	int position;
	char ldatac;
	int ldatai;
	char* ldatas;
	double ldatad;
	list_t* tmp_list;
	printf ( "printing list:\n" );
	position = 0;
	tmp_list = _list;
	do
	{

		printf ( "list position %i:\t", position );
		switch ( _data_type )
		{
			case CHAR_T: case UNSIGNED_CHAR_T:
				ldatac = *( char* )list_getdata ( tmp_list );
				printf ( "data: %c\n", ldatac);
			break;
			case DOUBLE_T: case FLOAT_T:
				ldatad = *( double* )list_getdata ( tmp_list );
				printf ( "data: %f\n", ldatad);
			break;
			case CHAR_POINTER_T:
				ldatas = ( char * )list_getdata ( tmp_list );
				printf( "data %s\n", ldatas);
			break;
			default:
				ldatai = *( int * )list_getdata( tmp_list );
				printf( "data %i\n", ldatai);
		}
		position++;
	}
	while (
		(tmp_list = list_gettail(tmp_list)));

}

Refactorings

No refactoring yet !

Avatar

chrome

January 15, 2008, January 15, 2008 11:47, permalink

No rating. Login to rate!

You could save a lot of effort if you used two structs, one for the header and one for each entry.

You could also save a bit of effort if you used calloc() instead of malloc().

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef struct list list;
typedef struct list_entry list_entry;

struct list_entry {
    list_entry          *prev;
    list_entry          *next;
    void                *object;
};

struct list {
    list_entry          *first;
    list_entry          *last;
};

Your refactoring





Format Copy from initial code

or Cancel