char *test_split() { mu_assert(List_count(list) == 4, "Wrong count before split."); List *a = List_create(); List *b = List_create(); List *tmp = List_copy(list); int rc = -1; rc = List_split(tmp, test2, a, b); mu_assert(rc == 0, "Failed to split."); mu_assert(List_count(a) == 2, "List 'a' has wrong count."); mu_assert(List_count(b) == 2, "List 'b' has wrong count."); List_destroy(a); List_destroy(b); a = List_create(); b = List_create(); tmp = List_copy(list); rc = List_split(tmp, test1, a, b); mu_assert(rc == 0, "Failed to split."); mu_assert(List_count(a) == 1, "List 'a' has wrong count."); mu_assert(List_count(b) == 3, "List 'b' has wrong count."); List_destroy(a); List_destroy(b); a = List_create(); b = List_create(); tmp = List_copy(list); rc = List_split(tmp, test3, a, b); mu_assert(rc == 0, "Failed to split."); mu_assert(List_count(a) == 3, "List 'a' has wrong count."); mu_assert(List_count(b) == 1, "List 'b' has wrong count."); List_destroy(a); List_destroy(b); a = List_create(); b = List_create(); tmp = List_copy(list); rc = List_split(tmp, test4, a, b); mu_assert(rc == 0, "Failed to split."); mu_assert(List_count(a) == 4, "List 'a' has wrong count."); mu_assert(List_count(b) == 0, "List 'b' has wrong count."); return NULL; }
/** * Merged two linked list together * * Arguments: * head1 A pointer pointing to linked list 1 * head2 A pointer pointing to linked list 2 * * Returns: * A merged sparse array * * This function will merge two linked lists. Before merging, you * should make a copy of "head1" so that you will still have the * original array while modifying (merging) the second linked list. * * Please refer to the README file for detailed instructions on how to * merge two lists. * * This function should not modify either "head1" or "head2". You only * need to make a clone of "head1". */ Node * List_merge(Node * head1, Node * head2) { Node * head3 = List_copy(head1); //copy of list1 while (head2 != NULL) { head3 = List_insert_ascend(head3,head2->value,head2->index); head2 = head2->next; } Node * copy = head3; //maintains pointer to top of list while deleting zero values Node * copy2 = head3; while(head3 != NULL) { if(head3->value == 0) { if(head3 == copy) { copy2 = head3->next; } head3 = List_delete(head3,head3->index); } head3 = head3->next; } if(head3 == NULL) { return copy2; } else { return head3; } }
List *List_duplicate(const void *fromList, const void *fromListFromNode, const void *fromListToNode, ListNodeCopyFunction listNodeCopyFunction, void *listNodeCopyUserData ) { List *list; assert(fromList != NULL); assert(listNodeCopyFunction != NULL); list = (List*)malloc(sizeof(List)); if (list == NULL) return NULL; List_init(list); List_copy(fromList, list, fromListFromNode, fromListToNode, NULL, listNodeCopyFunction, listNodeCopyUserData ); return list; }
void PatternList_copy(const PatternList *fromPatternList, PatternList *toPatternList) { assert(fromPatternList != NULL); assert(toPatternList != NULL); List_copy(fromPatternList,toPatternList,NULL,NULL,NULL,(ListNodeCopyFunction)copyPatternNode,NULL); }
int main(int argc, char ** argv) { if (argc != 3) { printf("Usage: ./pa05 <input file 1> <input file 2>\n"); return EXIT_FAILURE; } // Input file processed, ready to build the sparse-array Node * array1 = NULL, * array2 = NULL; int okay = TRUE; okay = okay && readSparseArrayFile(argv[1], &array1); okay = okay && readSparseArrayFile(argv[2], &array2); if(okay) { FILE * out = stdout; // This is where we output results printf("*******************************\n"); printf("Array 1:\n"); printf("*******************************\n"); List_print(out, array1); printf("\n*******************************\n"); printf("Array 2:\n"); printf("*******************************\n"); List_print(out, array2); printf("\n*******************************\n"); printf("Copy of Array 1:\n"); printf("*******************************\n"); Node * clone_array = List_copy(array1); List_print(out, clone_array); printf("\n*******************************\n"); printf("Merged array:\n"); printf("*******************************\n"); Node * array_new = List_merge(array1, array2); List_print(out, array_new); printf("\n\nArray destroy sequence 1\n\n"); // List_destroy(array_new); // List_destroy(clone_array); } printf("\n\nArray destroy sequence 2\n\n"); fflush(stdout); // List_destroy(array1); // List_destroy(array2); return EXIT_SUCCESS; }
void EntryList_copy(const EntryList *fromEntryList, EntryList *toEntryList, const EntryNode *fromEntryListFromNode, const EntryNode *fromEntryListToNode ) { assert(fromEntryList != NULL); assert(toEntryList != NULL); List_copy(fromEntryList,toEntryList,fromEntryListFromNode,fromEntryListToNode,NULL,(ListNodeCopyFunction)copyEntryNode,NULL); }
/** * Merged two linked list together * * Arguments: * head1 A pointer pointing to linked list 1 * head2 A pointer pointing to linked list 2 * * Returns: * A merged sparse array * * This function will merge two linked lists. Before merging, you * should make a copy of "head1" so that you will still have the * original array while modifying (merging) the second linked list. * * Please refer to the README file for detailed instructions on how to * merge two lists. * * This function should not modify either "head1" or "head2". You only * need to make a clone of "head1". */ Node * List_merge(Node * head1, Node * head2) { Node * newList = List_copy(head1); while(head2 != NULL) { newList = List_insert_ascend(newList, head2->value, head2->index); head2 = head2->next; } return newList; }
/** * Merged two linked list together * * Arguments: * head1 A pointer pointing to linked list 1 * head2 A pointer pointing to linked list 2 * * Returns: * A merged sparse array * * This function will merge two linked lists. Before merging, you * should make a copy of "head1" so that you will still have the * original array while modifying (merging) the second linked list. * * Please refer to the README file for detailed instructions on how to * merge two lists. * * This function should not modify either "head1" or "head2". You only * need to make a clone of "head1". */ Node * List_merge(Node * head1, Node * head2) { Node * headcopy = NULL; headcopy = List_copy(head1); //Node * head1copy = List_copy(head1);//why is it unused? while(head2 != NULL) { if(head2->value != 0) { /* if((headcopy -> index) == (head2 -> index)) { //Node * headmerged =, should i make a new variable headmerged? headcopy->value = (headcopy->value) + (head2->value); if ((headcopy -> value) == 0) { List_delete(headcopy, (headcopy->index)); } } else if (headcopy->index != head2->index) { if(head2->value != 0) { headcopy = List_insert_ascend(headcopy, head2->value, head2->index); } //headcopy->next = List_create(head2->index, head2-> //if((headcopy->value) == 0) // { // List_delete(headcopy, headcopy->index); // } } else if(head2 == NULL) { headcopy = List_copy(head1); if(headcopy->value == 0) { List_delete(headcopy, headcopy->index); } } */ headcopy = List_insert_ascend(headcopy, head2->value, head2->index); if(headcopy->value == 0) { headcopy = List_delete(headcopy, headcopy->index); } } //head1 = head1 ->next; head2 = head2 ->next; } return headcopy; }
int List_copy_test(List *list) { List *copy; copy = List_copy(list); assert(copy); // demonstrate deepness of the copy *(int *)copy->first->next->value *= 2; assert(*(int *)list->first->next->value != *(int *)copy->first->next->value); List_destroy(copy); return 0; }
char* test_copy() { src = List_create(); dest = List_create(); List_push(src, test1); List_push(src, test2); List_push(src, test3); List_copy(dest, src); mu_assert(List_count(dest) == 3, "Wrong copy - count."); mu_assert(List_first(dest) == test1, "Wrong copy - first."); mu_assert(List_last(dest) == test3, "Wrong copy - last."); return NULL; }
int main (int argc, char * argv[]) { printf("make a node and print it"); Node * test = List_create(5,1); List_print(stdout, test); printf("destroying it"); List_destroy(test); List_print(stdout, test); printf("linkingit again"); Node * newnode =NULL; Node * newnode2 = NULL; int values[4] = {2,3,1,2}; int indexs[4] = {1,4,2,3}; // int i; int length = 4; printf("this is newnode"); newnode = List_build(values, indexs, length); //printf("this is the newnode"); //List_print(stdout, newnode); //printf("address:%p", newnode->next); //printf("address:%p", newnode->next->next); newnode2 = List_copy(newnode); printf("copy of new node"); List_print(stdout, newnode2); //List_print(stdout, newnode); Node * merged = NULL; merged = List_merge(newnode, newnode2); List_print(stdout, merged); // printf("copy linked list"); /* while (newnode!= NULL) { List_print(stdout, newnode); printf("this is the address : %p", newnode->next); } */ //newnode = List_insert_ascend(newnode -> next,3,4); //List_print(stdout, newnode); //printf("this is the address : %p", newnode->next); //List_print(stdout, newnode -> next); return 0; }
/** * Merged two linked list together * * Arguments: * head1 A pointer pointing to linked list 1 * head2 A pointer pointing to linked list 2 * * Returns: * A merged sparse array * * This function will merge two linked lists. Before merging, you * should make a copy of "head1" so that you will still have the * original array while modifying (merging) the second linked list. * * Please refer to the README file for detailed instructions on how to * merge two lists. * * This function should not modify either "head1" or "head2". You only * need to make a clone of "head1". */ Node * List_merge(Node * head1, Node * head2) { Node* p = List_copy(head1); Node* a1 = head2; Node* a2 = p; while(a1 != NULL){ if(a1->index == a2->index){ al->value += a2->value; } else{ List_insert_ascend(a1, a2->value, a2->index); } a1 = a1->next; } return p; }
char *test_copy() { list = List_create(); mu_assert(List_count(list) == 0, "Wrong count before copy."); List_push(list, test1); List_push(list, test2); List_push(list, test3); List_push(list, test4); mu_assert(List_count(list) == 4, "Wrong count after push."); List *copy = List_copy(list); mu_assert(copy != list, "Copy and list have same address."); mu_assert(List_count(copy) == 4, "Copy has wrong count."); return NULL; }
int main() { List a, b; int32 i, j; List_init(&a); List_init(&b); print_list(&a, "a"); for (i = 0; i < 10; i++) { a.insert(&a, 2*i); } print_list(&a, "a"); a.erase(&a); for (i = 0, j = 7; i < 10; i++, j = (j+7)%10) { print_list(&a, "a"); a.insert(&a, 2*j); } print_list(&a, "a"); print_list(&b, "b"); for (i = 5; i > 0; i--) { b.insert(&b, i); } print_list(&b, "b"); List_copy(&b, &a); print_list(&a, "a"); print_list(&b, "b"); a.remove(&a, 0); a.remove(&a, 18); a.remove(&a, 7); a.remove(&a, 8); print_list(&a, "a"); print_list(&b, "b"); b.erase(&b); print_list(&b, "b"); List_done(&a); List_done(&b); return OK; }
Node * List_copy(Node * head) { Node * headcopy; if(head == NULL) { return NULL; } else { headcopy = List_create(head->value, head->index); headcopy -> next = List_copy(head -> next); } //can i do this? // headcopy = List_create(head->value, head->index); // headcopy -> next = head -> next; // head = head -> next; // } return headcopy; }
char *test_copy() { List_copy(list); return NULL; }