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

@@ -80,6 +80,35 @@ void list_append(struct linked_list* list, void* data_ptr)
list->size++;
}
void list_append_list(struct linked_list* list, struct linked_list* other)
{
/* Anything to move? */
if (!other->size)
return;
if (!list->size)
{
/* If the list is empty, just move the pointers */
list->size = other->size;
list->first = other->first;
list->last = other->last;
list->iterator = other->iterator;
}
else
{
other->first->prev = list->last;
list->last->next = other->first;
list->last = other->last;
list->size += other->size;
}
/* Make sure the original list appears empty */
other->size = 0;
other->first = NULL;
other->last = NULL;
other->iterator = NULL;
}
void list_remove(struct linked_list* list, void* data_ptr)
{
@@ -113,6 +142,30 @@ void list_remove(struct linked_list* list, void* data_ptr)
}
}
void list_remove_first(struct linked_list* list, void (*free_handle)(void* ptr))
{
struct node* node = list->first;
list->iterator = NULL;
if (!node)
return;
list->first = node->next;
if (list->first)
list->first->prev = NULL;
if (list->last == node)
list->last = NULL;
list->size--;
if (free_handle)
free_handle(node->ptr);
hub_free(node);
}
size_t list_size(struct linked_list* list)
{

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