void list_join (void **list1, void **list2) { struct list_head *tail1, *head2; ASSERT (list_is_head (list1)); ASSERT (list_is_head (list2)); tail1 = (struct list_head *) list_get_tail (list1); if (tail1 == NULL) { *list1 = *list2; return; } tail1->next = LIST_HEAD (*list2); }
int main(int argc, char **argv) { List list; ListElmt *element; int *data, i; /***************************************************************************** * * * Initialize the linked list. * * * *****************************************************************************/ list_init(&list, free); /***************************************************************************** * * * Perform some linked list operations. * * * *****************************************************************************/ element = list_head(&list); for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i; if (list_ins_next(&list, NULL, data) != 0) return 1; } print_list(&list); element = list_head(&list); for (i = 0; i < 7; i++) element = list_next(element); data = list_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; if (list_ins_next(&list, list_tail(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Removing an element after the first element\n"); element = list_head(&list); if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 012 at the head of the list\n"); *data = 12; if (list_ins_next(&list, NULL, data) != 0) return 1; print_list(&list); fprintf(stdout, "Iterating and removing the fourth element\n"); element = list_head(&list); element = list_next(element); element = list_next(element); if (list_rem_next(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 013 after the first element\n"); *data = 13; if (list_ins_next(&list, list_head(&list), data) != 0) return 1; print_list(&list); i = list_is_head(&list, list_head(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i); i = list_is_head(&list, list_tail(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (0=OK)\n", i); i = list_is_tail(list_tail(&list)); fprintf(stdout, "Testing list_is_tail...Value=%d (1=OK)\n", i); i = list_is_tail(list_head(&list)); fprintf(stdout, "Testing list_is_tail...Value=%d (0=OK)\n", i); /***************************************************************************** * * * Destroy the linked list. * * * *****************************************************************************/ fprintf(stdout, "Destroying the list\n"); list_destroy(&list); return 0; }
int test_linked_list() { int i,j, result = 0; List_Node *test_node1 = NULL; List_Node *test_node2 = NULL; List_Node *test_node3 = NULL; List_Node *pTrack[5]; void* pArr[6]; List_Head *test_list1 = list_new(); List_Head *test_list2 = list_new(); test_msg_start("Test Linked List Creation"); if(test_list1 == NULL) result++; test_msg_end(result); test_msg_start("Test Linked List Empty Length - Using Variable"); if(test_list1->count != 0) result++; test_msg_end(result); test_msg_start("Test Linked List Empty Length - Using Length Function"); if(list_len(test_list1) != 0 ) result++; test_msg_end(result); test_msg_start("Test Linked List - Adding Node To Empty List"); if(list_ins_tail(test_list1) == NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Adding Node To Non-Empty List"); if(list_ins_tail(test_list1) == NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Clearing List With More Than One Node"); list_clear(test_list1); if(test_list1->count != 0) result++; if(list_len(test_list1) != 0 ) result++; test_msg_end(result); test_msg_start("Test Linked List - Clearing List With No Nodes"); list_clear(test_list1); if(test_list1->count != 0) result++; if(list_len(test_list1) != 0 ) result++; test_msg_end(result); test_msg_start("Test Linked List End - No Nodes"); list_clear(test_list1); if(list_tail(test_list1) != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List End - One Node Only"); list_clear(test_list1); test_node1 = list_ins_tail(test_list1); if(test_node1 == NULL) result++; if(list_tail(test_list1) != test_node1) result++; test_msg_end(result); test_msg_start("Test Linked List End - More Than One Node"); list_clear(test_list1); list_ins_tail(test_list1); test_node1 = list_ins_tail(test_list1); if(test_node1 == NULL) result++; if(list_tail(test_list1) != test_node1) result++; test_msg_end(result); test_msg_start("Test Linked List - Removing Node From List With More Than One Node"); list_clear(test_list1); test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_head(test_list1); list_rm_node(test_list1, test_node1); if(test_list1->pNext != test_node2) result++; test_msg_end(result); test_msg_start("Test Linked List - Removing Node From Empty List"); list_clear(test_list1); test_node1 = (List_Node*)&test_list1; /* pointer points to known bad location */ /* should not crash but return gracefully */ if(list_rm_node(test_list1, test_node1) != -1) result++; test_msg_end(result); test_msg_start("Test Linked List - Inserting Node After"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_after(test_list1, test_node1); /* test beginning */ if(test_list1->pNext != test_node1) result++; /*...and end nodes. */ if(list_tail(test_list1) != test_node2) result++; /* end node next should be null */ if(list_tail(test_list1)->pNext != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Inserting Node Before"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; test_node1 = list_ins_head(test_list1); test_node2 = list_ins_before(test_list1, test_node1); /* test beginning */ if(test_list1->pNext != test_node2) result++; /*...and end nodes. */ if(list_tail(test_list1) != test_node1) result++; /* end node next should be null */ if(list_tail(test_list1)->pNext != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Test Lengths"); list_clear(test_list1); if(list_len(test_list1) != 0) result++; /* adding nodes using each function */ list_ins_head(test_list1); if(list_len(test_list1) != 1) result++; list_ins_tail(test_list1); if(list_len(test_list1) != 2) result++; list_ins_before(test_list1, list_tail(test_list1)); if(list_len(test_list1) != 3) result++; list_ins_after(test_list1, list_tail(test_list1)); if(list_len(test_list1) != 4) result++; test_msg_end(result); test_msg_start("Test Linked List - Previous Node Check"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; if(list_prev_node(test_list1, NULL) != NULL) result++; if(list_prev_node(NULL, test_list1->pNext) != NULL) result++; if(list_prev_node(NULL, test_node1 + 1000) != NULL) result++; test_node1 = list_ins_head(test_list1); if(list_prev_node(test_list1, test_list1->pNext) != NULL) result++; test_node2 = list_ins_head(test_list1); if(list_prev_node(test_list1, test_node1) != test_node2) result++; if(list_prev_node(test_list1, test_node2) != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Removing Nodes By Address"); /* removing nodes from various places */ list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; if(list_rm_node(NULL, test_list1->pNext)!= -1) result++; if(list_rm_node(test_list1, NULL) != -1) result++; test_node1 = list_ins_head(test_list1); test_node2 = list_ins_head(test_list1); list_rm_node(test_list1, test_list1->pNext); if(list_len(test_list1) != 1) result++; test_node1 = list_tail(test_list1); list_rm_node(test_list1, test_node1); if(list_len(test_list1) != 0) result++; if(list_rm_node(test_list1, list_tail(test_list1)) != -1) result++; test_msg_end(result); test_msg_start("Test Linked List - Removing Nodes After Address"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); if(list_rm_next(test_list1, test_node1) != 1) result++; test_msg_end(result); test_msg_start("Test Linked List - Removing Nodes Before Address"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; if(list_rm_before(test_list1, NULL) != -1) result++; if(list_rm_before(NULL, NULL) != -1) result++; if(list_rm_before(test_list1 + 1000, NULL) != -1) result++; test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); if(list_rm_before(test_list1, test_node2 + 1000) != -1) result++; if(list_rm_before(test_list1, test_node2) != 1) result++; test_msg_end(result); test_msg_start("Test Linked List - Copying List"); list_clear(test_list1); list_clear(test_list2); test_node1 = list_ins_tail(test_list1); test_node1->pData = &result; test_node2 = list_ins_tail(test_list1); if(list_copy(test_list2, test_list1) != 0) result++; if(test_list1->pNext == test_list2->pNext) result++; if(test_list1->count != 2) result++; if(test_list2->count != 2) result++; if(test_list1->pNext->pData != test_list2->pNext->pData) result++; test_msg_end(result); test_msg_start("Test Linked List - Copying Empty List"); list_clear(test_list1); list_clear(test_list2); if(list_copy(test_list2, test_list1) != 0) result++; if(test_list1->pNext != NULL) result++; if(test_list2->pNext != NULL) result++; if(test_list1->count != 0) result++; if(test_list2->count != 0) result++; test_msg_end(result); test_msg_start("Test Linked List - Get Node Address By Node Number - First Node"); list_clear(test_list1); test_node1 = list_ins_head(test_list1); test_node2 = list_ins_head(test_list1); test_node3 = list_get_num(test_list1, 1); if(test_node3 != test_node2) result++; test_msg_end(result); test_msg_start("Test Linked List - Get Node Address By Node Number - Middle Node"); list_clear(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); test_node3 = list_get_num(test_list1, 5); if(test_node3 != test_node2) result++; test_msg_end(result); test_msg_start("Test Linked List - Get Node Address By Node Number - Last Node"); list_clear(test_list1); test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); test_node3 = list_get_num(test_list1, 2); if(test_node3 != test_node2) result++; test_msg_end(result); test_msg_start("Test Linked List - Get Node Address By Node Number - Empty List"); list_clear(test_list1); test_node1 = list_get_num(test_list1, 1); if(test_node1 != NULL) result++; test_node1 = list_get_num(test_list1, 55); if(test_node1 != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Swap Nodes In List"); list_clear(test_list1); test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); test_node3 = list_ins_tail(test_list1); pTrack[1] = test_node1->pNext; pTrack[2] = test_node2->pNext; if(list_node_swap(test_node1, test_node2) != 0) result++; if(test_node1->pNext != pTrack[2]) result++; if(test_node2->pNext != pTrack[1]) result++; test_msg_end(result); test_msg_start("Test Linked List - List Reverse - Pointer Tracking"); list_clear(test_list1); for(i = 0; i < 5; i++) { pTrack[i] = list_ins_tail(test_list1); pTrack[i]->pData = &pTrack[i]->pData; } test_list1 = list_reverse(test_list1); for(i = 1, j = 4; i <= 4; i++, j--) { test_node1 = list_get_num(test_list1, i); if(test_node1->pData != pTrack[j]) result++; } test_msg_end(result); test_msg_start("Test Linked List - List Next Preprocessor"); list_clear(test_list1); list_clear(test_list2); test_node1 = NULL; test_node2 = NULL; test_node1 = list_ins_head(test_list1); test_node2 = list_ins_head(test_list1); if(list_next(test_node1) == test_node2) result++; test_msg_end(result); test_msg_start("Test Linked List - Default Payload"); list_clear(test_list1); test_node1 = NULL; test_node2 = NULL; test_node1 = list_ins_tail(test_list1); test_node2 = list_ins_tail(test_list1); if(test_node1->pData != NULL) result++; /* make data point to something and test again... */ test_node1->pData = test_node1; if(list_data(test_node1) != test_node1) result++; test_msg_end(result); test_msg_start("Test Linked List - List Is Head Preprocessor"); if(list_is_head(test_list1, test_node2) != 0) result++; if(list_is_head(test_list1, test_node1) != 1) result++; test_msg_end(result); test_msg_start("Test Linked List - List Is Tail Preprocessor"); if(list_is_tail(test_node2) != 1) result++; if(list_is_tail(test_node1) != 0) result++; test_msg_end(result); test_msg_start("Test Linked List - List Head Preprocessor"); if(list_head(test_list1) != test_node1) result++; test_msg_end(result); test_msg_start("Test Linked List - Append Lists - Pointer Tracking"); list_clear(test_list1); list_clear(test_list2); list_ins_tail(test_list1); list_ins_tail(test_list1); list_ins_tail(test_list1); pTrack[0] = list_ins_tail(test_list1); pTrack[1] = list_ins_tail(test_list2); list_ins_tail(test_list2); list_ins_tail(test_list2); list_append(test_list1, test_list2); if(test_list1->count != 7) result++; if(!list_search(test_list1, pTrack[0])) result++; if(!list_search(test_list1, pTrack[1])) result++; if(pTrack[0] != list_get_num(test_list1, 4)) result++; if(pTrack[1] != list_get_num(test_list1, 5)) result++; test_msg_end(result); test_msg_start("Test Linked List - Create Data Array - Pointer Tracking"); list_clear(test_list1); pTrack[0] = list_ins_tail(test_list1); pTrack[0] = pTrack[0]->pData = &test_list1; pTrack[1] = list_ins_tail(test_list1); pTrack[1] = pTrack[1]->pData = &test_list2; pTrack[2] = list_ins_tail(test_list1); pTrack[2] = pTrack[2]->pData = &pTrack; pTrack[3] = list_ins_tail(test_list1); pTrack[3] = pTrack[3]->pData = &test_node1; pTrack[4] = list_ins_tail(test_list1); pTrack[4] = pTrack[4]->pData = &test_node2; pArr[5] = &test_list1; if(list_data_array(test_list1, pArr, 6) != 0) result++; if(pArr[0] != pTrack[0]) result++; if(pArr[1] != pTrack[1]) result++; if(pArr[2] != pTrack[2]) result++; if(pArr[3] != pTrack[3]) result++; if(pArr[4] != pTrack[4]) result++; if(pArr[5] != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Create Node Array - Pointer Tracking"); list_clear(test_list1); pTrack[0] = list_ins_tail(test_list1); pTrack[1] = list_ins_tail(test_list1); pTrack[2] = list_ins_tail(test_list1); pTrack[3] = list_ins_tail(test_list1); pTrack[4] = list_ins_tail(test_list1); pArr[5] = &test_list1; if(list_node_array(test_list1, pArr, 6) != 0) result++; if(pArr[0] != pTrack[0]) result++; if(pArr[1] != pTrack[1]) result++; if(pArr[2] != pTrack[2]) result++; if(pArr[3] != pTrack[3]) result++; if(pArr[4] != pTrack[4]) result++; if(pArr[5] != NULL) result++; test_msg_end(result); test_msg_start("Test Linked List - Search List"); list_clear(test_list1); if(list_search(test_list1, test_node1) != 0) result++; test_node1 = list_ins_head(test_list1); if(list_search(test_list1, test_node1) != 1) result++; if(list_search(test_list1, test_node2) != 0) result++; if(list_search(test_list1, test_node3) != 0) result++; if(list_search(test_list1, NULL) != 0) result++; if(list_search(test_list1, (List_Node *)test_list1) != 0) result++; test_node2 = list_ins_head(test_list1); if(list_search(test_list1, test_node1) != 1) result++; if(list_search(test_list1, test_node2) != 1) result++; if(list_search(test_list1, test_node3) != 0) result++; if(list_search(test_list1, NULL) != 0) result++; if(list_search(test_list1, (List_Node *)test_list1) != 0) result++; list_clear(test_list1); if(list_search(test_list1, test_node1) != 0) result++; if(list_search(test_list1, test_node2) != 0) result++; if(list_search(test_list1, test_node3) != 0) result++; if(list_search(test_list1, NULL) != 0) result++; if(list_search(test_list1, (List_Node *)test_list1) != 0) result++; test_msg_end(result); return result; }
int main() { List list; ListElmt *element; int *data; int i; list_init(&list, free); element = list_head(&list); for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) { return 1; } * data = i; if (list_ins_next(&list, NULL, data) != 0) { return 1; } } print_list(&list); element = list_head(&list); for (i = 0; i < 7; i++) { element = list_next(element); } data = list_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); if (list_rem_next(&list, element, (void **)&data) != 0) { return 1; } print_list(&list); fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; if (list_ins_next(&list, list_tail(&list), data) != 0) { return 1; } print_list(&list); fprintf(stdout, "Removing an element after the first element\n"); element = list_head(&list); if (list_rem_next(&list, element, (void **)&data) != 0) { return 1; } print_list(&list); fprintf(stdout, "Inserting 012 at the head of the list\n"); *data = 12; if (list_ins_next(&list, NULL, data) != 0) { return 1; } print_list(&list); fprintf(stdout, "Iterating and removing the fourth element\n"); element = list_head(&list); element = list_next(element); element = list_next(element); if(list_rem_next(&list, element, (void **)&data) != 0) { return 1; } print_list(&list); fprintf(stdout, "Inserting 013 after the first element\n"); *data = 13; if (list_ins_next(&list, list_head(&list), data) != 0) { return 1; } print_list(&list); i = list_is_head(&list, list_head(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i); i = list_is_head(&list, list_tail(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i); i = list_is_tail(list_tail(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i); i = list_is_tail(list_head(&list)); fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i); fprintf(stdout, "Destroying the list\n"); list_destroy(&list); return 0; }
int main(int argc, char **argv) { Graph graph; MstVertex *mst_start, *mst_vertex, mst_vertex1, *mst_vertex2; PathVertex *pth_start, *pth_vertex, pth_vertex1, *pth_vertex2; TspVertex *tsp_start, *tsp_vertex; List span, paths, vertices, tour; ListElmt *element; double distance, total, x, y; int i; /***************************************************************************** * * * Compute a minimum spanning tree. * * * *****************************************************************************/ graph_init(&graph, match_mst, free); fprintf(stdout, "Computing a minimum spanning tree\n"); for (i = 0; i < MSTVCT; i++) { if ((mst_vertex = (MstVertex *)malloc(sizeof(MstVertex))) == NULL) return 1; if (i == 0) mst_start = mst_vertex; mst_vertex->data = MstTestV[i]; if (graph_ins_vertex(&graph, mst_vertex) != 0) return 1; } for (i = 0; i < MSTECT; i++) { if ((mst_vertex2 = (MstVertex *)malloc(sizeof(MstVertex))) == NULL) return 1; mst_vertex1.data = MstTestE[i].vertex1; mst_vertex2->data = MstTestE[i].vertex2; mst_vertex2->weight = MstTestE[i].weight; if (graph_ins_edge(&graph, &mst_vertex1, mst_vertex2) != 0) return 1; } print_graph_mst(&graph); if (mst(&graph, mst_start, &span, match_mst) != 0) return 1; for (element = list_head(&span); element != NULL; element = list_next(element)) { mst_vertex = list_data(element); fprintf(stdout, "vertex=%s, parent=%s\n", (char *)mst_vertex->data, mst_vertex->parent != NULL ? (char *)mst_vertex->parent->data : "*"); } list_destroy(&span); graph_destroy(&graph); /***************************************************************************** * * * Compute shortest paths. * * * *****************************************************************************/ graph_init(&graph, match_pth, free); fprintf(stdout, "Computing shortest paths\n"); for (i = 0; i < PTHVCT; i++) { if ((pth_vertex = (PathVertex *)malloc(sizeof(PathVertex))) == NULL) return 1; if (i == 1) pth_start = pth_vertex; pth_vertex->data = PthTestV[i]; if (graph_ins_vertex(&graph, pth_vertex) != 0) return 1; } for (i = 0; i < PTHECT; i++) { if ((pth_vertex2 = (PathVertex *)malloc(sizeof(PathVertex))) == NULL) return 1; pth_vertex1.data = PthTestE[i].vertex1; pth_vertex2->data = PthTestE[i].vertex2; pth_vertex2->weight = PthTestE[i].weight; if (graph_ins_edge(&graph, &pth_vertex1, pth_vertex2) != 0) return 1; } print_graph_pth(&graph); if (shortest(&graph, pth_start, &paths, match_pth) != 0) return 1; for (element = list_head(&paths); element != NULL; element = list_next(element)) { pth_vertex = list_data(element); fprintf(stdout, "vertex=%s, parent=%s, d=%.1lf\n", (char *)pth_vertex-> data, pth_vertex->parent != NULL ? (char *)pth_vertex->parent->data : "*", pth_vertex->d); } list_destroy(&paths); graph_destroy(&graph); /***************************************************************************** * * * Solve the traveling-salesman problem. * * * *****************************************************************************/ list_init(&vertices, free); fprintf(stdout, "Solving a traveling-salesman problem\n"); for (i = 0; i < TSPVCT; i++) { if ((tsp_vertex = (TspVertex *)malloc(sizeof(TspVertex))) == NULL) return 1; if (i == 0) tsp_start = tsp_vertex; tsp_vertex->data = TspTestV[i].vertex; tsp_vertex->x = TspTestV[i].x; tsp_vertex->y = TspTestV[i].y; if (list_ins_next(&vertices, list_tail(&vertices), tsp_vertex) != 0) return 1; } print_list_tsp(&vertices); if (tsp(&vertices, tsp_start, &tour, match_tsp) != 0) return 1; total = 0; for (element = list_head(&tour); element != NULL; element = list_next(element)) { tsp_vertex = list_data(element); if (!list_is_head(&tour, element)) { distance = sqrt(pow(tsp_vertex->x-x, 2.0) + pow(tsp_vertex->y-y, 2.0)); total = total + distance; } x = tsp_vertex->x; y = tsp_vertex->y; if (!list_is_head(&tour, element)) { fprintf(stdout, "vertex=%s, distance=%.2lf\n", (char *)tsp_vertex-> data, distance); } else fprintf(stdout, "vertex=%s\n", (char *)tsp_vertex->data); } fprintf(stdout, "total=%.2lf\n", total); list_destroy(&vertices); list_destroy(&tour); return 0; }