void *clist_remove_last(CList *list) { CListNode *node = list->first; // no element if (!node) return NULL; //only one element if (list->first == list->last) return clist_pop(list); while ((node->next != NULL) && (node->next != list->last)) node = node->next; CListNode *old_last = node->next; void *value = old_last->value; node->next = NULL; list->last = node; free(old_last); return value; }
void test_clist_pop(void) { struct key k1; struct key k2; struct key k3; k1.id = 1; k2.id = 2; k3.id = 3; struct clist list; int ret = clist_init(&list, match_function, empty_destroy_function, CLIST_UNSORTED); CU_ASSERT(ret == CLIST_TRUE); ret = clist_append(&list, (void *)&k1); CU_ASSERT(ret == CLIST_TRUE); ret = clist_append(&list, (void *)&k2); CU_ASSERT(ret == CLIST_TRUE); ret = clist_append(&list, (void *)&k3); CU_ASSERT(ret == CLIST_TRUE); struct key *data; /* struct clist_node * n = list.head; */ ret = clist_get_by_id(&list, (void **)&data, -1); CU_ASSERT(ret == CLIST_TRUE); if (ret == CLIST_TRUE) CU_ASSERT(data->id == 3); ret = clist_pop(&list); CU_ASSERT(ret == CLIST_TRUE); ret = clist_get_by_id(&list, (void **)&data, -1); CU_ASSERT(ret == CLIST_TRUE); if (ret == CLIST_TRUE) CU_ASSERT(data->id == 2); clist_destroy(&list); }