Added list_at function

This commit is contained in:
Walter Julius Hennecke 2013-04-12 23:52:21 +02:00
parent ae24153bb5
commit 1a66b0f08b
2 changed files with 28 additions and 0 deletions

View file

@ -337,3 +337,22 @@ void list_init(struct list * l, void (*destructor)(void*)) {
l->destructor = destructor;
}
container_p list_at(list_p list, int idx) {
list_iter_p iter;
container_p c = NULL;
int i;
if(idx < 0 || idx >= list->length || list == NULL) {
return NULL;
}
iter = list_iterator(list, LIST_FRONT);
for(c = list_next(iter), i = 0; c != NULL; c = list_next(iter), i++) {
if(i == idx) {
break;
}
}
return c;
}

View file

@ -324,4 +324,13 @@ void list_clear(list_p list);
*/
void list_init(struct list * l, void (*destructor)(void*));
/**
* Get the element at the given index.
*
* \param list a list
* \param idx index
* \return element at given index or NULL if index is out of bounds
*/
container_p list_at(list_p list, int idx);
#endif