linked_list: add pop and get_first_item operations

This commit is contained in:
Matthias Ringwald 2016-11-07 13:08:44 +01:00
parent 43ab8f9679
commit 67711d5ef9
3 changed files with 32 additions and 6 deletions

View File

@ -95,11 +95,6 @@ void btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_i
it->next = item;
}
/**
* Remove data_source from run loop
*
* @note: assumes that btstack_data_source_t.next is first element in data_source
*/
int btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item){ // <-- remove item from list
if (!item) return -1;
btstack_linked_item_t *it;
@ -124,6 +119,20 @@ int btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_ite
return counter;
}
// get first element
btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list){
return * list;
}
// pop (get + remove) first element
btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list){
btstack_linked_item_t * item = *list;
if (!item) return NULL;
*list = item->next;
return item;
}
//
// Linked List Iterator implementation
//

View File

@ -64,9 +64,13 @@ int btstack_linked_list_empty(btstack_linked_list_t * list);
// add item to list as first element
void btstack_linked_list_add(btstack_linked_list_t * list, btstack_linked_item_t *item);
// add item to list as last element
void btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item);
void btstack_linked_list_add_tail(btstack_linked_list_t * list, btstack_linked_item_t *item);
// pop (get + remove) first element
btstack_linked_item_t * btstack_linked_list_pop(btstack_linked_list_t * list);
// remove item from list
int btstack_linked_list_remove(btstack_linked_list_t * list, btstack_linked_item_t *item);
// get first element
btstack_linked_item_t * btstack_linked_list_get_first_item(btstack_linked_list_t * list);
// find the last item in the list
btstack_linked_item_t * btstack_linked_list_get_last_item(btstack_linked_list_t * list);

View File

@ -32,6 +32,19 @@ TEST(LinkedList, CountAll){
CHECK_EQUAL(4, btstack_linked_list_count(&testList));
}
TEST(LinkedList, GetFirst){
btstack_linked_item_t * item;
item = btstack_linked_list_get_first_item(&testList);
CHECK_EQUAL(item, &itemA);
}
TEST(LinkedList, Pop){
btstack_linked_item_t * item;
item = btstack_linked_list_pop(&testList);
CHECK_EQUAL(item, &itemA);
CHECK_EQUAL(3, btstack_linked_list_count(&testList));
}
TEST(LinkedList, Iterator){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &testList);