list: add convenience function to get item by index
This commit is contained in:
parent
715fec034b
commit
2cddc37a67
@ -42,6 +42,7 @@ extern node_t * list_pop(list_t * list);
|
||||
extern node_t * list_dequeue(list_t * list);
|
||||
extern list_t * list_copy(list_t * original);
|
||||
extern void list_merge(list_t * target, list_t * source);
|
||||
extern void * list_index(list_t * list, int index);
|
||||
|
||||
extern void list_append_after(list_t * list, node_t * before, node_t * node);
|
||||
extern node_t * list_insert_after(list_t * list, node_t * before, void * item);
|
||||
|
@ -166,6 +166,15 @@ int list_index_of(list_t * list, void * value) {
|
||||
return -1; /* not find */
|
||||
}
|
||||
|
||||
void * list_index(list_t * list, int index) {
|
||||
int i = 0;
|
||||
foreach(item, list) {
|
||||
if (i == index) return item->value;
|
||||
i++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void list_remove(list_t * list, size_t index) {
|
||||
/* remove index from the list */
|
||||
if (index > list->length) return;
|
||||
|
Loading…
Reference in New Issue
Block a user