This commit is contained in:
mykola2312 2022-04-16 16:44:10 +03:00
parent 21132d5c2c
commit a2bfa2361d
3 changed files with 14 additions and 2 deletions

9
list.c
View file

@ -55,3 +55,12 @@ void list_remove(list_t* list, list_node_t* node)
list->count--; list->count--;
cu_free(node); 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;
}

1
list.h
View file

@ -25,6 +25,7 @@ void list_clear(list_t* list);
void list_add(list_t* list, uint size, void* data); void list_add(list_t* list, uint size, void* data);
void list_remove(list_t* list, list_node_t* node); 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) \ #define CU_LIST_FOREACH(list) \
for(list_node_t* i = (list)->first; i; i = i->next) for(list_node_t* i = (list)->first; i; i = i->next)

6
test.c
View file

@ -11,13 +11,15 @@ int main()
srand(time(NULL)); srand(time(NULL));
for (uint i = 0; i < 5; i++) for (uint i = 0; i < 5; i++)
{ {
uint num = rand(); uint num = i;//rand();
list_add(&list, sizeof(uint), &num); list_add(&list, sizeof(uint), &num);
} }
list_remove(&list, list_at(&list, 2));
CU_LIST_FOREACH(&list) 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); list_clear(&list);