char *test_linked_list_last() { struct ll_node *last = LL_last(head); test_assert(last, "Did not get last node"); test_assert_streq(last->val, val3, "linked list last node has wrong val"); return NULL; }
struct ll_node *LL_add_tail(struct ll_node *head, void *val) { struct ll_node *last = LL_last(head); struct ll_node *n = LL_new(val); check(n, "Could not allocate new node"); last->next = n; return n; error: return NULL; }
char *test_linked_list_delete() { int length = LL_length(head); // Deleting the last node LL_delete(&head, val3); test_assert(LL_length(head) == (length - 1), "Wrong length after deletion"); struct ll_node *last = LL_last(head); test_assert(last->val != val3, "linked list last node has wrong val after deletion"); // Deleting the head node LL_delete(&head, val4); test_assert(LL_length(head) == (length - 2), "Wrong length after deletion"); test_assert(head->val != val4, "linked list head node has wrong val after deletion"); return NULL; }
void LL_reverse_test(int count) { nodePtr head = newTestLL(count), n, newHead; for (n = head, newHead = NULL; n; n = n->next) { if (n) LL_append(&newHead, n->value); } int results = 1; head = LL_reverse(head, NULL); results &= newHead->value == LL_last(head)->value; // Reverse back to how it was before this function. head = LL_reverse(head, NULL); nodePtr tempHead = head, tempNewHead = newHead; do results &= tempNewHead->value == tempHead->value; while ((tempNewHead = tempNewHead->next) && (tempHead = tempHead->next)); processTestResults("reverse", results); LL_freeAll(&head); }