int main(int argc, char **argv) { //Array statistics double array[5] = {2.0, 3.89, -3.94, 10.1, 0.88}; print_array_stats(array, 5); //Creating liked list with 3 3s and 4 4s linked_list *ll3 = new_linked_list(3, 3); linked_list *ll4 = new_linked_list(4, 4); //Should print: "3 3 3" print_linked_list(ll3, 1, 1); //Inserting a 5 at the 1st position insert_linked_list(ll3, 1, 5); //Should print "3 5 3 3" print_linked_list(ll3, 1, 1); //Printing backwards, should print: "3 3 5 3" print_linked_list(ll3, 1, 0); //Merging the linked lists merge_linked_list(ll3, ll4); //Printing the result, should print: "3 4 5 4 3 4 3 4" print_linked_list(ll3, 1, 1); //Summing the elements, should be 30 printf("Sum: %d\n", sum_linked_list(ll3)); //Freeing the memory of ll3 destroy_linked_list(ll3); }
void FrontBackSplit(struct node* source, struct node** forntRef, struct node** backRef) { struct node* fast = NULL; struct node* slow = NULL; if(source == NULL || source->next == NULL) { *forntRef = source; *backRef = NULL; } else { slow = source; fast = source->next; while(fast != NULL) { fast = fast->next; if(fast != NULL) { slow = slow->next; fast = fast->next; } } } *backRef = slow->next; *forntRef = source; slow->next = NULL; print_linked_list("FBS", forntRef); print_linked_list("BBS", backRef); // whiel() }
int main(int argc, char const *argv[]) { struct node *head = NULL; // struct node *head2 = NULL; // struct node *head_front = NULL; // struct node *head_back = NULL; // int retval = 0; // head1 = create_linked_list(); // head2 = create_linked_list(); Push(&head, 10); Push(&head, 9); Push(&head, 8); Push(&head, 7); Push(&head, 6); Push(&head, 5); Push(&head, 4); Push(&head, 3); Push(&head, 2); Push(&head, 1); // Push(&head2, 12); // Push(&head2, 10); // Push(&head2, 2); // Push(&head2, 4); // Push(&head2, 6); // Push(&head_back, 10); // Push(&head_back, 8); // Push(&head_back, 1); // retval = Pop(&head); // printf("[%d]\n",retval); print_linked_list("main", &head); // print_linked_list("head_back", &head_back); // FrontBackSplit(head, &head_front, &head_back); // head = ShuffleMerge(head_back, head_front); // head = SortedMerge(head_back, head_front); // head = SortedIntersect(head_front, head_back); MergeSort(&head); // RecursiveReverse(&head); // gfg_q8(&head, 2, 3); // gfg_q1(&head); // gfg_q4(&head); print_linked_list("main", &head); // print_linked_list("head2", &head2); // gfg_q6(&head1, &head2); // print_linked_list("head1", &head1); // print_linked_list("head2", &head2); // AlternatingSplit(head,&head_front, &head_back); // free_linked_list(head_front); // free_linked_list(head_back); free_linked_list(head); // free_linked_list(head2); return 0; }
int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; std::vector<int> this_vec = {0,1,2,3,4,5,6}; print_vec(this_vec); reverse_vector(this_vec); print_vec(this_vec); std::vector<int> big_vector = fill_vec(47); print_vec(big_vector); reverse_vector(big_vector); print_vec(big_vector); std::list<int> small_list = fill_list(15, 47); print_list(small_list); reverse_list(small_list); print_list(small_list); std::list<int> big_list = fill_list(100, 710); print_list(big_list); reverse_list(big_list); print_list(big_list); Node* a = new Node; a->value = 6; a->ptr = new Node; a->ptr->value = 7; a->ptr->ptr = new Node; a->ptr->ptr->value = 8; a->ptr->ptr->ptr = new Node; a->ptr->ptr->ptr->value = 9; a->ptr->ptr->ptr->ptr = NULL; // print out this list print_linked_list("a",a); // create an STL list with 4 elements std::list<int> b; b.push_back(10); b.push_back(11); b.push_back(12); b.push_back(13); // use the STL list as input to a creator function that creates // linked lists with the same data Node* c = make_linked_list_from_STL_list(b); // print that data print_linked_list("c",c); // // WRITE A FEW MORE TEST CASES OF make_linked_list_from_STL_list // // reverse a linked list of nodes Node* d = reverse_nodes_in_linked_list(c); // print this data print_linked_list("d",d); // // WRITE A FEW MORE TEST CASES OF reverse_nodes_in_linked_list return 0; }
int main ( int argc, char * args[]) { node *head = NULL; head = delete_at_end(head); head = delete_at_first(head); head = delete_at_position(head, 66); head = insert_at_position (head, 2, 5 ); head = insert_at_first (head, 4 ); head = insert_at_position (head, 2, 3 ); head = insert_at_position (head, 2, 2 ); head = insert_at_position (head, 2, 1 ); print_linked_list (head); head = delete_at_first(head); head = delete_at_end(head); head = delete_at_position (head, 2); head = delete_at_position (head, 2); head = delete_at_position (head, 2); print_linked_list (head); printf ("the length is %d\n" ,length_linked_list(head)); //printf ("successfully deleted list : %d\n", purge_linked_list(head)); return SUCCESS; }
void gfg_q4(struct node** headRef) { struct node *fast = *headRef; struct node *slow = *headRef; struct node *temp = NULL; if(slow == NULL) return; if(slow->next == NULL) return; fast = slow->next; temp = fast->next; fast->next = slow; slow->next = temp; *headRef = fast; print_linked_list("g4g_q4", headRef); while(slow != NULL && slow->next != NULL) { if(slow->next->next == NULL) { break; } fast = slow->next->next; temp = fast->next; slow->next->next = temp; fast->next = slow->next; slow->next = fast; slow= slow->next->next; print_linked_list("g4g_q4_iter", headRef); } }
void MergeSort(struct node** headRef) { struct node *head = *headRef; struct node *head_front = NULL; struct node *head_back = NULL; if(head == NULL || head->next == NULL) { printf("return\n"); return; } FrontBackSplit(head, &head_front, &head_back); print_linked_list("&head_front", &head_front); MergeSort(&head_front); print_linked_list("&head_back", &head_back); MergeSort(&head_back); *headRef = SortedMerge(head_front, head_back); print_linked_list("headRef", headRef); }
int main(void) { Node *head1 = NULL, *head2 = NULL , *new_list = NULL; int sorted_arr1[] = {80};//{6,6,6}; //{ 11,17,25,29,31,59,102, 115, 167 }; int sorted_arr2[] = { 6,17,29,45,59,59,59 }; int sorted_arr1_size = ( sizeof( sorted_arr1 ) / sizeof(int) ); int sorted_arr2_size = ( sizeof( sorted_arr2 ) / sizeof(int) ); create_list( &head1, sorted_arr1, sorted_arr1_size ); create_list( &head2, sorted_arr2, sorted_arr2_size ); merge_sorted_lists( &new_list , head1 , head2 ); printf("\nThe sorted linked list-1 is : \n"); print_linked_list( head1); printf("\n\nThe sorted linked list-2 is : \n"); print_linked_list( head2); printf("\n\nThe merged list is : \n"); print_linked_list( new_list ); printf("\n\n"); return 0; }
int main() { Linked_List *linked_list = NULL; int error_code; int i; for(i = 0; i < SIZE_OF_LIST; ++i) { error_code = add_linked_list(&linked_list, i); if(error_code) { fprintf(stderr, "\nError. Adding item (%d) failed.\n", i); return EXIT_FAILURE; } } print_linked_list(linked_list); printf("\nReversing list..."); error_code = reverse_linked_list(&linked_list); if(error_code) { fprintf(stderr, "\nError. Reversing list failed.\n"); } print_linked_list(linked_list); printf("\nRemoving 3rd node from end of list..."); error_code = remove_nth_from_end(&linked_list, 3); if(error_code) { fprintf(stderr, "\nError. Removing nth node list failed.\n"); } print_linked_list(linked_list); destroy_linked_list(linked_list); return EXIT_SUCCESS; }
void AlternatingSplit(struct node* source,struct node** aRef, struct node** bRef) { while(source != NULL) { MoveNode(aRef, &source); if(source != NULL) MoveNode(bRef, &source); } print_linked_list("aRef", aRef); print_linked_list("bRef", bRef); }
struct node *create_linked_list() { int i = 0; //int element[NO_ELEMENTS] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; // int element[NO_ELEMENTS] = {1,2,3,4,5}; // (0,10)-> (1,10)-> (3,10)-> (10,10)-> (10,8)-> (10,5)-> (20,5)-> (40,5) int element[NO_ELEMENTS] = {1,2,3,4,5,6,7}; // int element2[NO_ELEMENTS] ={10,10,10,10, 8, 5, 5, 5}; struct node* head_ptr; struct node *new_node; struct node *temp_node; head_ptr = malloc(sizeof(struct node)); temp_node = head_ptr; head_ptr->next = NULL; head_ptr->data = element[0]; // head_ptr->data2 = element2[0]; for(i = 1; i < NO_ELEMENTS; i++) { new_node = malloc(sizeof(struct node)); temp_node->next = new_node; new_node->data = element[i]; // new_node->data2 = element2[i]; new_node->next = NULL; temp_node = new_node; } print_linked_list("create_linked_list", &head_ptr); return head_ptr; }
void* malloc(size_t size) { struct __malloc_link* header; void* candidate; if (size == 0) return NULL; #ifdef DEBUG fprintf(stdout, "+ malloc %zu bytes requested.\n", size); fprintf(stdout, "\tcurrent sbrk: %p\n", sbrk(0)); #endif candidate = sbrk(size + sizeof(struct __malloc_link)); /* failed */ if (candidate < 0) return candidate; /* setup header */ header = (struct __malloc_link*) candidate; header->start = candidate + sizeof(struct __malloc_link); /* refresh head of list */ header->next = __MALLOC_GLOBAL_HEAD; __MALLOC_GLOBAL_HEAD = header; #ifdef DEBUG print_linked_list(__MALLOC_GLOBAL_HEAD); #endif return header->start; }
int main(int argc, char* argv[]) { int arr[10] = {1,2,3,3,1,1,3,3,5,6}; s_node* root = create_linked_list(arr,10); remove_dup(root); print_linked_list(root); clear_linked_list(root); return 0; }
int main(int argc, char* argv[]) { int a[] = {1,2,3,4,5}; s_node* root = create_linked_list(a,sizeof(a)/sizeof(a[0])); s_node* r = get_element(root, 3); print_linked_list(r); clear_linked_list(root); return 0; }
void print_transition_table(TRANSITIONTABLE* ptt) { int i; for(i = 0; i < ptt -> n; i++) { printf("idx %d, length %d: ", i, ptt -> parray[i].length); print_linked_list((ptt -> parray) + i); putchar('\n'); } }
int main() { auto l1 = make_linked_list<ListNode, int>({9,9}); auto l2 = make_linked_list<ListNode, int>({5,6,2}); Solution so; auto l3 = so.addTwoNumbers(l1, l2); print_linked_list(l3); return 0; }
void print_linked_list( Node* head ) { if( head == NULL ) { return; } else { printf("%d\t",head->data); print_linked_list( head->next ); } }
struct node* SortedMerge(struct node* a, struct node* b) { struct node dummy; struct node *head = &dummy; dummy.next = NULL; while(1) { print_linked_list("a",&a); print_linked_list("b",&b); print_linked_list("head",&dummy.next); if(a == NULL) { head->next = b; break; } if(b == NULL) { head->next = a; break; } if(a->data <= b->data) { printf("a <= b\n"); MoveNode(&head->next, &a); } else { printf("a > b\n"); MoveNode(&head->next, &b); } head = head->next; } return dummy.next; }
void no_of_ways_of_n_stairs(int no_of_steps, NODE* list) { if(no_of_steps==0) { list = print_linked_list(list); return; } else if(no_of_steps==1) { list = append_node(list,'S'); no_of_ways_of_n_stairs(0,list); list = delete_node(list); return; } else if(no_of_steps==2) { list = append_node(list,'S'); list = append_node(list,'S'); list = print_linked_list(list); list = delete_node(list); list = delete_node(list); list = append_node(list,'D'); list = print_linked_list(list); list = delete_node(list); return; } else if(no_of_steps>2) { list = append_node(list,'D'); no_of_ways_of_n_stairs(no_of_steps-2,list); list = delete_node(list); list = append_node(list,'S'); no_of_ways_of_n_stairs(no_of_steps-1,list); list = delete_node(list); return; } }
void Reverse(struct node** headRef) { struct node *result = NULL; struct node *current = *headRef; struct node *next = NULL; while(current != NULL) { next = current->next; current->next = result; result = current; current = next; print_linked_list("result", &result); } *headRef = result; }
int main(int argc, const char * argv[]) { //first, we create the linked list header node *root = malloc(sizeof(node)); node *end = malloc(sizeof(node)); root->next = end; end->prev = root; end->next = NULL; node *current_node = root; int index; int list_size = 100; //then make linked list of 350 //this will be our usable coefficient buffer for (index = 0; index<list_size; index++){ int random_number = arc4random() % 150; random_number = (random_number%2)? random_number*-1: random_number; node *new_node = malloc(sizeof(node)); new_node->coeff_struct.coefficient = random_number; add_to_linked_list(new_node, current_node); current_node = current_node->next; } printf("\n"); printf("loop coefficients are: "); //print_linked_list(root); printf("\n"); char *message = "Hello";/* that whirl me I know not whither\nYour schemes, politics, fail, lines give way, substances mock and elude me,\nOnly the theme I sing, the great and strong-possess'd soul, eludes not,\nOne's-self must never give way--that is the final substance--that\nout of all is sure,\nOut of politics, triumphs, battles, life, what at last finally remains?\nWhen shows break up what but One's-Self is sure?";*/ embedMessageIntoCoefficients(message, root, list_size); printf("\n\nour stego coefficients: \n"); print_linked_list(root); printf("\n"); int message_size = (int)strlen(message)*8; //getting size of message in bits char *extractedMessage = malloc(sizeof(char) *message_size); extractMessageFromCoefficients(root, list_size, message_size, extractedMessage); return 0; }
void free(void* pointer) { struct __malloc_link* prev = __MALLOC_GLOBAL_HEAD; struct __malloc_link* current = __MALLOC_GLOBAL_HEAD; #ifdef DEBUG fprintf(stdout, "+ free address 0x%p\n", pointer); #endif while (current && (current->start != pointer)) { prev = current; current = current->next; #ifdef DEBUG fprintf(stdout, "\twhile loop, checking current->next == %p\n", current->next); #endif } #ifdef DEBUG fprintf(stdout, "\tmatched: start = %p, pointer = %p\n", current->start, pointer); #endif if (current && current->start) { /* remove from linked list */ if (current == __MALLOC_GLOBAL_HEAD) { __MALLOC_GLOBAL_HEAD = current->next; brk(current); } else prev->next = current->next; } #ifdef DEBUG print_linked_list(__MALLOC_GLOBAL_HEAD); #endif return; }
/* * Tests */ int linked_list_test(){ /** * Entry point for tests */ printf("Linked List Tests:\n\n"); int array[] = {1, 2, 3, 4, 42, 11, 6845}; LinkedList *list = create_linked_list_from_array(sizeof(int), array, 7); print_linked_list(list); int testval1 = 42; int testval2 = 47; printf("%d\n", linked_list_contains(list, &testval1)); printf("%d\n", linked_list_contains(list, &testval2)); delete_linked_list(list); return 0; }
int main(void) { int i; int array_size = 5; int array[] = {4, 6, 2, 9, 7}; ListNode *head; ListNode *previous = NULL; for (i = 0; i < array_size; ++i) { head = malloc(sizeof(ListNode)); assert(head != NULL); head->data = array[i]; head->next = previous; previous = head; } print_linked_list(head); return 0; }
void test_intersects() { int i; node *ll1, *ll2, *ll3, *tmp; ll1 = ll2 = ll3 = NULL; declare_linked_list(ll1, "first", 8); declare_linked_list(ll2, "second", 12); declare_linked_list(ll3, "third", 5); tmp->next = ll1->next->next->next->next; printf("The third list with intersection looks like: "); print_linked_list(ll3); printf( "Asserting the first linked list does not intersect with the second\n" ); assert(find_intersection(ll1, ll2) == NULL); printf( "Asserting the second linked list does not intersect with the first\n" ); assert(find_intersection(ll2, ll1) == NULL); printf( "Asserting the second linked list does not intersect with the third\n" ); assert(find_intersection(ll2, ll3) == NULL); printf( "Asserting the third linked list does not intersect with the second\n" ); assert(find_intersection(ll3, ll2) == NULL); printf( "Asserting the first linked list intersects with the third\n" ); assert(find_intersection(ll1, ll3) == tmp->next); printf( "Asserting the third linked list intersects with the first\n" ); assert(find_intersection(ll1, ll3) == tmp->next); }
int main(void) { IntNode *head = NULL; // An empty linked list. IntNode *empty = NULL; // Another empty linked list. printf("=== Testing insert_front ===\n\n"); printf("Calling insert_front with list: "); print_linked_list(head); printf("\nInserting 3.\n"); head = insert_front(head, 3); printf("Expected list: 3\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("Calling insert_front with list: "); print_linked_list(head); printf("\nInserting 2.\n"); head = insert_front(head, 2); printf("Expected list: 2 -> 3\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("Calling insert_front with list: "); print_linked_list(head); printf("\nInserting 1.\n"); head = insert_front(head, 1); printf("Expected list: 1 -> 2 -> 3\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("=== Testing contains ===\n\n"); _Bool found; printf("Calling contains with list: "); print_linked_list(empty); printf("\nSearching for 1.\n"); found = contains(empty, 1); printf("Expected result: false\n"); printf("Actual result: "); print_boolean(found); printf("\n\n"); printf("Calling contains with list: "); print_linked_list(head); printf("\nSearching for 1.\n"); found = contains(head, 1); printf("Expected result: true\n"); printf("Actual result: "); print_boolean(found); printf("\n\n"); printf("Calling contains with list: "); print_linked_list(head); printf("\nSearching for 3.\n"); found = contains(head, 3); printf("Expected result: true\n"); printf("Actual result: "); print_boolean(found); printf("\n\n"); printf("Calling contains with list: "); print_linked_list(head); printf("\nSearching for 6.\n"); found = contains(head, 6); printf("Expected result: false\n"); printf("Actual result: "); print_boolean(found); printf("\n\n"); printf("=== Testing append_rear ===\n\n"); printf("Calling append_rear with list: "); print_linked_list(head); printf("\nAppending 4.\n"); head = append_rear(head, 4); printf("Expected list: 1 -> 2 -> 3 -> 4\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("=== Testing remove_first ===\n\n"); printf("Calling remove_first with list: "); print_linked_list(head); head = remove_first(head); printf("\nExpected list: 2 -> 3 -> 4\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("=== Testing remove_last ===\n\n"); printf("Calling remove_last with list: "); print_linked_list(head); head = remove_last(head); printf("\nExpected list: 2 -> 3\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("Calling remove_last with list: "); print_linked_list(head); head = remove_last(head); printf("\nExpected list: 2\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); printf("Calling remove_last with list: "); print_linked_list(head); head = remove_last(head); printf("\nExpected list: empty list\n"); printf("Actual list: "); print_linked_list(head); printf("\n\n"); /* Tests for Exercise 1. */ printf("Building linked list 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 5 -> 5\n\n"); head = NULL; head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(4, head); head = intnode_construct(3, head); head = intnode_construct(3, head); head = intnode_construct(2, head); head = intnode_construct(1, head); head = intnode_construct(1, head); // print_linked_list(head); printf("=== Testing count ===\n\n"); int occurrences; printf("Calling count with list: "); print_linked_list(empty); printf("\nCounting 1's.\n"); occurrences = count(empty, 1); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(empty); printf("\nCounting 7's.\n"); occurrences = count(empty, 7); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 1's.\n"); occurrences = count(head, 1); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 2's.\n"); occurrences = count(head, 2); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 3's.\n"); occurrences = count(head, 3); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 4's.\n"); occurrences = count(head, 4); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 5's.\n"); occurrences = count(head, 5); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 7's.\n"); occurrences = count(head, 7); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); /* Tests for Exercise 2. */ printf("=== Testing index ===\n\n"); int posn; printf("Calling index with list: "); print_linked_list(empty); printf("\nSearching for 1.\n"); posn = index(empty, 1); printf("Expected result: -1\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 1.\n"); posn = index(head, 1); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 2.\n"); posn = index(head, 2); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 3.\n"); posn = index(head, 3); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 4.\n"); posn = index(head, 4); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 5.\n"); posn = index(head, 5); printf("Expected result: 6\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 7.\n"); posn = index(head, 7); printf("Expected result: -1\n"); printf("Actual result: %d\n\n", posn); /* Tests for Exercise 31. */ printf("=== Testing fetch ===\n\n"); /* We can't test these cases, because they should cause the function * to terminate via assert. * * 1. The list is empty; terminate via assert. * 2. index < 0 or index >= # of nodes; terminate via assert. */ int value; printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 0.\n"); value = fetch(head, 0); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 1.\n"); value = fetch(head, 1); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 2.\n"); value = fetch(head, 2); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 3.\n"); value = fetch(head, 3); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 4.\n"); value = fetch(head, 4); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 5.\n"); value = fetch(head, 5); printf("Expected result: 4\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 6.\n"); value = fetch(head, 6); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 7.\n"); value = fetch(head, 7); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 8.\n"); value = fetch(head, 8); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); /* Tests for Exercise 4. */ printf("Building linked list 1 -> 2 -> 3 -> 4\n\n"); IntNode *list = NULL; list = intnode_construct(4, list); list = intnode_construct(3, list); list = intnode_construct(2, list); list = intnode_construct(1, list); printf("=== Testing remove_last_one_pointer ===\n\n"); printf("Calling remove_last_one_pointer with list: "); print_linked_list(list); list = remove_last_one_pointer(list); printf("\nExpected list: 1 -> 2 -> 3\n"); printf("Actual list: "); print_linked_list(list); printf("\n\n"); printf("Calling remove_last_one_pointer with list: "); print_linked_list(list); list = remove_last_one_pointer(list); printf("\nExpected list: 1 -> 2\n"); printf("Actual list: "); print_linked_list(list); printf("\n\n"); printf("Calling remove_last_one_pointer with list: "); print_linked_list(list); list = remove_last_one_pointer(list); printf("\nExpected list: 1\n"); printf("Actual list: "); print_linked_list(list); printf("\n\n"); printf("Calling remove_last_one_pointer with list: "); print_linked_list(list); list = remove_last_one_pointer(list); printf("\nExpected list: empty list\n"); printf("Actual list: "); print_linked_list(list); printf("\n\n"); }
int main(void) { IntNode *head; /* Tests for Exercise 1. */ printf("=== Testing remove_duplicates ===\n\n"); printf("Test 1\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(5, head); head = intnode_construct(3, head); head = intnode_construct(3, head); head = intnode_construct(4, head); head = intnode_construct(4, head); head = intnode_construct(4, head); head = intnode_construct(1, head); head = intnode_construct(3, head); head = intnode_construct(3, head); print_linked_list(head); printf("\n"); printf("Calling remove_duplicates\n"); remove_duplicates(head); printf("Expected result: 3 -> 1 -> 4 -> 3 -> 5\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 2\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(5, head); print_linked_list(head); printf("\n"); printf("Calling remove_duplicates\n"); remove_duplicates(head); printf("Expected result: 5\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 3\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(2, head); head = intnode_construct(2, head); print_linked_list(head); printf("\n"); printf("Calling remove_duplicates\n"); remove_duplicates(head); printf("Expected result: 2\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 4\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(4, head); head = intnode_construct(3, head); head = intnode_construct(2, head); head = intnode_construct(1, head); print_linked_list(head); printf("\n"); printf("Calling remove_duplicates\n"); remove_duplicates(head); printf("Expected result: 1 -> 2 -> 3 -> 4\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 5\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(4, head); print_linked_list(head); printf("\n"); printf("Calling remove_duplicates\n"); remove_duplicates(head); printf("Expected result: 4\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); /* Tests for Exercise 2. */ printf("=== Testing reverse ===\n\n"); printf("Test 1\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(4, head); head = intnode_construct(3, head); head = intnode_construct(2, head); head = intnode_construct(1, head); print_linked_list(head); printf("\n"); printf("Calling reverse\n"); head = reverse(head); printf("Expected result: 4 -> 3 -> 2 -> 1\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 2\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(2, head); head = intnode_construct(1, head); print_linked_list(head); printf("\n"); printf("Calling reverse\n"); head = reverse(head); printf("Expected result: 2 -> 1\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 3\n"); printf("Building linked list: "); head = NULL; head = intnode_construct(2, head); print_linked_list(head); printf("\n"); printf("Calling reverse\n"); head = reverse(head); printf("Expected result: 2\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); printf("Test 4\n"); printf("Building linked list: "); head = NULL; print_linked_list(head); printf(" (head == NULL)\n"); printf("Calling reverse\n"); head = reverse(head); printf("Expected result: empty list\n"); printf(" Actual result: "); print_linked_list(head); printf("\n\n"); free_linked_list(head); }
int main(void) { IntNode *head = NULL; // An empty linked list. IntNode *empty = NULL; // Another empty linked list. /* Tests for Exercise 1. */ printf("Building linked list 1 -> 1 -> 2 -> 3 -> 3 -> 4 -> 5 -> 5 -> 5\n\n"); head = NULL; head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(5, head); head = intnode_construct(4, head); head = intnode_construct(3, head); head = intnode_construct(3, head); head = intnode_construct(2, head); head = intnode_construct(1, head); head = intnode_construct(1, head); // print_linked_list(head); printf("=== Testing count ===\n\n"); int occurrences; printf("Calling count with list: "); print_linked_list(empty); printf("\nCounting 1's.\n"); occurrences = count(empty, 1); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(empty); printf("\nCounting 7's.\n"); occurrences = count(empty, 7); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 1's.\n"); occurrences = count(head, 1); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 2's.\n"); occurrences = count(head, 2); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 3's.\n"); occurrences = count(head, 3); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 4's.\n"); occurrences = count(head, 4); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 5's.\n"); occurrences = count(head, 5); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", occurrences); printf("Calling count with list: "); print_linked_list(head); printf("\nCounting 7's.\n"); occurrences = count(head, 7); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", occurrences); /* Tests for Exercise 2. */ printf("=== Testing index ===\n\n"); int posn; printf("Calling index with list: "); print_linked_list(empty); printf("\nSearching for 1.\n"); posn = index(empty, 1); printf("Expected result: -1\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 1.\n"); posn = index(head, 1); printf("Expected result: 0\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 2.\n"); posn = index(head, 2); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 3.\n"); posn = index(head, 3); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 4.\n"); posn = index(head, 4); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 5.\n"); posn = index(head, 5); printf("Expected result: 6\n"); printf("Actual result: %d\n\n", posn); printf("Calling index with list: "); print_linked_list(head); printf("\nSearching for 7.\n"); posn = index(head, 7); printf("Expected result: -1\n"); printf("Actual result: %d\n\n", posn); /* Tests for Exercise 3. */ printf("=== Testing fetch ===\n\n"); /* We can't test these cases, because they should cause the function * to terminate via assert. * * 1. The list is empty; terminate via assert. * 2. index < 0 or index >= # of nodes; terminate via assert. */ int value; printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 0.\n"); value = fetch(head, 0); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 1.\n"); value = fetch(head, 1); printf("Expected result: 1\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 2.\n"); value = fetch(head, 2); printf("Expected result: 2\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 3.\n"); value = fetch(head, 3); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 4.\n"); value = fetch(head, 4); printf("Expected result: 3\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 5.\n"); value = fetch(head, 5); printf("Expected result: 4\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 6.\n"); value = fetch(head, 6); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 7.\n"); value = fetch(head, 7); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); printf("Calling fetch with list: "); print_linked_list(head); printf("\nFetching value at index 8.\n"); value = fetch(head, 8); printf("Expected result: 5\n"); printf("Actual result: %d\n\n", value); /* Tests for Exercise 4. */ printf("=== Testing insert ===\n\n"); IntNode *list1 = NULL; // An empty linked list. printf("Calling insert with list: "); print_linked_list(list1); printf("\nInserting 10 at index 0.\n"); list1 = insert(list1, 0, 10); printf("Expected list:\t10\n"); printf("Actual list:\t"); print_linked_list(list1); printf("\n\n"); printf("Calling insert with list: "); print_linked_list(list1); printf("\nInserting 20 value at index 0.\n"); list1 = insert(list1, 0, 20); printf("Expected list:\t20 -> 10\n"); printf("Actual list:\t"); print_linked_list(list1); printf("\n\n"); printf("Calling insert with list: "); print_linked_list(list1); printf("\nInserting 30 at index 1.\n"); list1 = insert(list1, 1, 30); printf("Expected list:\t20 -> 30 -> 10\n"); printf("Actual list:\t"); print_linked_list(list1); printf("\n\n"); printf("Calling insert with list: "); print_linked_list(list1); printf("\nInserting 40 at index 3.\n"); list1 = insert(list1, 3, 40); printf("Expected list:\t20 -> 30 -> 10 -> 40\n"); printf("Actual list:\t"); print_linked_list(list1); printf("\n\n"); }