diff --git a/list.c b/list.c index 1e2e420..a62ee7d 100644 --- a/list.c +++ b/list.c @@ -55,3 +55,12 @@ void list_remove(list_t* list, list_node_t* node) list->count--; cu_free(node); } + +list_node_t* list_at(list_t* list, uint idx) +{ + list_node_t* node = list->first; + if (!idx) return node; + else if (idx >= list->count) return NULL; + while (idx--) node = node->next; + return node; +} diff --git a/list.h b/list.h index 955a13f..0802b18 100644 --- a/list.h +++ b/list.h @@ -25,6 +25,7 @@ void list_clear(list_t* list); void list_add(list_t* list, uint size, void* data); void list_remove(list_t* list, list_node_t* node); +list_node_t* list_at(list_t* list, uint idx); #define CU_LIST_FOREACH(list) \ for(list_node_t* i = (list)->first; i; i = i->next) diff --git a/test.c b/test.c index 567455c..8139ff8 100644 --- a/test.c +++ b/test.c @@ -11,13 +11,15 @@ int main() srand(time(NULL)); for (uint i = 0; i < 5; i++) { - uint num = rand(); + uint num = i;//rand(); list_add(&list, sizeof(uint), &num); } + list_remove(&list, list_at(&list, 2)); CU_LIST_FOREACH(&list) { - printf("%p\t%d\n", i, LIST_NODE_VALUE(i, uint)); + printf("%p\t%p\t%p\t%d\n", i->prev, i, i->next, + LIST_NODE_VALUE(i, uint)); } list_clear(&list);