struct LList_t *llist_add_node(struct LList_t **head, int id, void *data) { struct LList_t *newnode = NULL; debug_printf("Adding new node id: %d\n", id); newnode = (struct LList_t *)malloc(sizeof(struct LList_t)); newnode->id = id; newnode->data = data; newnode->next = NULL; //llist_print_node(newnode); llist_insert_node(head, newnode); /* do { if (ptr == NULL || ptr->next == NULL) { debug_printf("Adding new node id: %d\n", id); newnode = (struct LList_t *)malloc(sizeof(struct LList_t)); newnode->id = id; newnode->data = data; newnode->next = NULL; if (ptr == NULL) { *head = newnode; ptr = *head; } else { ptr->next = newnode; return newnode; } } ptr = ptr->next; } while (ptr != NULL); */ return newnode; }
END_TEST START_TEST ( llist_06_insert_nodes ) { int retval; llist listToTest = NULL; llist_node retptr; listToTest = llist_create ( NULL, trivial_equal, test_mt ? MT_SUPPORT_FALSE : MT_SUPPORT_TRUE ); // Insert a 5 nodes 1..5 retval = llist_add_node ( listToTest, ( llist_node ) 1, ADD_NODE_FRONT ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_add_node ( listToTest, ( llist_node ) 2, ADD_NODE_FRONT ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_add_node ( listToTest, ( llist_node ) 3, ADD_NODE_FRONT ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_add_node ( listToTest, ( llist_node ) 4, ADD_NODE_FRONT ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_add_node ( listToTest, ( llist_node ) 5, ADD_NODE_FRONT ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding nodes: " ); print_llist ( listToTest ); // Find the middle node (3) retval = llist_find_node ( listToTest, ( llist_node ) 3, &retptr ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); // Add node before retval = llist_insert_node ( listToTest, ( llist_node ) 7, retptr, ADD_NODE_BEFORE ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding before 3 node: " ); print_llist ( listToTest ); // Add node after retval = llist_insert_node ( listToTest, ( llist_node ) 8, retptr, ADD_NODE_AFTER ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding after 3 node: " ); print_llist ( listToTest ); // insert node at the start of the list (before the first node) retval = llist_find_node ( listToTest, ( llist_node ) 5, &retptr ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_insert_node ( listToTest, ( llist_node ) 9, retptr, ADD_NODE_BEFORE ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding 9 before the first node: " ); print_llist ( listToTest ); // insert node at the start of the list (after the first node) retval = llist_find_node ( listToTest, ( llist_node ) 9, &retptr ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_insert_node ( listToTest, ( llist_node ) 10, retptr, ADD_NODE_AFTER ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding 10 after the first node: " ); print_llist ( listToTest ); // insert node at the end of the list (after the first node) retval = llist_find_node ( listToTest, ( llist_node ) 1, &retptr); ck_assert_int_eq ( retval, LLIST_SUCCESS ); retval = llist_insert_node ( listToTest, ( llist_node ) 11, retptr, ADD_NODE_AFTER ); ck_assert_int_eq ( retval, LLIST_SUCCESS ); printf ( "List after adding 11 after the last node: " ); print_llist ( listToTest ); llist_destroy ( listToTest, false, NULL ); }