[list] Fix some bugs in that. It never works on the first push anyway, right?

This commit is contained in:
Kevin Lange 2011-11-28 23:45:33 -06:00
parent ea7ddfe61d
commit 05b59d366f
2 changed files with 17 additions and 0 deletions

View File

@ -28,6 +28,8 @@ void list_insert(list_t * list, void * item) {
/* Insert an item into a list */
node_t * node = malloc(sizeof(node_t));
node->value = item;
node->next = NULL;
node->prev = NULL;
if (!list->tail) {
list->head = node;
} else {
@ -74,6 +76,17 @@ void list_delete(list_t * list, node_t * node) {
}
}
void * list_pop(list_t * list) {
/* Remove and return the last value in the list
* If you don't need it, you still probably want to free it!
* Try free(list_pop(list)); !
* */
if (!list->tail) return NULL;
void * out = list->tail->value;
list_delete(list, list->tail);
return out;
}
list_t * list_copy(list_t * original) {
/* Create a new copy of original */
list_t * out = list_create();

View File

@ -28,4 +28,8 @@ void list_remove(list_t * list, size_t index);
void list_delete(list_t * list, node_t * node);
void * list_pop(list_t * list);
list_t * list_copy(list_t * original);
#define foreach(i, list) for (node_t * i = list->head; i != NULL; i = i->next)