Improvements to lists

This commit is contained in:
Kevin Lange 2013-03-17 16:32:44 -07:00
parent 3b5b532a27
commit 71931b3cf3
2 changed files with 19 additions and 4 deletions

View File

@ -38,13 +38,15 @@ void list_append(list_t * list, node_t * node) {
list->length++;
}
void list_insert(list_t * list, void * item) {
node_t * 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;
list_append(list, node);
return node;
}
void list_append_after(list_t * list, node_t * before, node_t * node) {
@ -70,12 +72,13 @@ void list_append_after(list_t * list, node_t * before, node_t * node) {
list->length++;
}
void list_insert_after(list_t * list, node_t * before, void * item) {
node_t * list_insert_after(list_t * list, node_t * before, void * item) {
node_t * node = malloc(sizeof(node_t));
node->value = item;
node->next = NULL;
node->prev = NULL;
list_append_after(list, before, node);
return node;
}
list_t * list_create() {
@ -96,6 +99,17 @@ node_t * list_find(list_t * list, void * value) {
return NULL;
}
int list_index_of(list_t * list, void * value) {
int i = 0;
foreach(item, list) {
if (item->value == value) {
return i;
}
i++;
}
return -1; /* not find */
}
void list_remove(list_t * list, size_t index) {
/* remove index from the list */
if (index > list->length) return;

View File

@ -22,9 +22,10 @@ typedef struct {
void list_destroy(list_t * list);
void list_free(list_t * list);
void list_append(list_t * list, node_t * item);
void list_insert(list_t * list, void * item);
node_t * list_insert(list_t * list, void * item);
list_t * list_create();
node_t * list_find(list_t * list, void * value);
int list_index_of(list_t * list, void * value);
void list_remove(list_t * list, size_t index);
void list_delete(list_t * list, node_t * node);
node_t * list_pop(list_t * list);
@ -33,7 +34,7 @@ list_t * list_copy(list_t * original);
void list_merge(list_t * target, list_t * source);
void list_append_after(list_t * list, node_t * before, node_t * node);
void list_insert_after(list_t * list, node_t * before, void * item);
node_t * list_insert_after(list_t * list, node_t * before, void * item);
#define foreach(i, list) for (node_t * i = list->head; i != NULL; i = i->next)