Cleaned up usage of linked lists and added missing functionality.

- Added a list_remove_first() which is generally better than list_remove()
  provided you want to remove the first element.
- Added a list_append_list() to append and move all nodes from one list to
  another.
This commit is contained in:
Jan Vidar Krey
2013-03-22 20:00:40 +01:00
parent b81bb2cbd9
commit 2d6f69d299
8 changed files with 170 additions and 24 deletions

View File

@@ -42,6 +42,13 @@ extern void list_clear(struct linked_list*, void (*free_handle)(void* ptr) );
extern void list_append(struct linked_list* list, void* data_ptr);
/**
* A special list append that moves all nodes from other_list to list.
* The other list will be empty.
*/
extern void list_append_list(struct linked_list* list, struct linked_list* other);
/**
* Remove data_ptr from the list. If multiple versions occur, only the first one is removed.
*/
@@ -57,6 +64,12 @@ extern void* list_get_prev(struct linked_list*);
extern struct node* list_get_first_node(struct linked_list*);
extern struct node* list_get_last_node(struct linked_list*);
/**
* Remove the first element, and call the free_handle function (if not NULL)
* to ensure the data is freed also.
*/
extern void list_remove_first(struct linked_list* list, void (*free_handle)(void* ptr));
#define LIST_FOREACH(TYPE, ITEM, LIST, BLOCK) \
for (ITEM = (TYPE) list_get_first(LIST); ITEM; ITEM = (TYPE) list_get_next(LIST)) \
BLOCK