added linked_list_add_tail to append at end of list

This commit is contained in:
matthias.ringwald 2010-07-29 18:23:08 +00:00
parent bb0b241aec
commit 7cd486c7a1
2 changed files with 21 additions and 7 deletions

View File

@ -46,7 +46,8 @@ typedef linked_item_t * linked_list_t;
void linked_item_set_user(linked_item_t *item, void *user_data); // <-- set user data void linked_item_set_user(linked_item_t *item, void *user_data); // <-- set user data
void * linked_item_get_user(linked_item_t *item); // <-- get user data void * linked_item_get_user(linked_item_t *item); // <-- get user data
void linked_list_add(linked_list_t * list, linked_item_t *item); // <-- add item to list void linked_list_add(linked_list_t * list, linked_item_t *item); // <-- add item to list as first element
void linked_list_add_tail(linked_list_t * list, linked_item_t *item); // <-- add item to list as last element
int linked_list_remove(linked_list_t * list, linked_item_t *item); // <-- remove item from list int linked_list_remove(linked_list_t * list, linked_item_t *item); // <-- remove item from list
void test_linked_list(); void test_linked_list();

View File

@ -53,6 +53,18 @@ void linked_list_add(linked_list_t * list, linked_item_t *item){ // <-- a
*list = item; *list = item;
} }
void linked_list_add_tail(linked_list_t * list, linked_item_t *item){ // <-- add item to list as last element
// check if already in list
linked_item_t *it;
for (it = (linked_item_t *) list; it->next ; it = it->next){
if (it->next == item) {
return;
}
}
item->next = (void*) 0;
it->next = item;
}
/** /**
* Remove data_source from run loop * Remove data_source from run loop
* *
@ -89,19 +101,20 @@ void test_linked_list(){
linked_item_set_user(&itemB, (void *) 0); linked_item_set_user(&itemB, (void *) 0);
linked_list_add(&testList, &itemA); linked_list_add(&testList, &itemA);
linked_list_add(&testList, &itemB); linked_list_add(&testList, &itemB);
linked_list_add(&testList, &itemC); linked_list_add_tail(&testList, &itemC);
// linked_list_remove(&testList, &itemB); // linked_list_remove(&testList, &itemB);
linked_item_t *it; linked_item_t *it;
for (it = (linked_item_t *) &testList; it ; it = it->next){ for (it = (linked_item_t *) &testList; it ; it = it->next){
if (it->next == &itemA) printf("Item A"); if (it->next == &itemA) printf("Item A\n");
if (it->next == &itemB) printf("Item B"); if (it->next == &itemB) printf("Item B\n");
if (it->next == &itemC) printf("Item C"); if (it->next == &itemC) printf("Item C\n");
if (it->next == &itemB){ /* if (it->next == &itemB){
it->next = it->next; it->next = it->next;
printf(" remove\n"); printf(" remove\n");
} else { } else {
printf(" keep\n"); printf(" keep\n");
}
*/
} }
} }
#endif #endif