NODE* sort_linked_list(NODE* head) { if(head == NULL || head->next == NULL) { return head; } NODE *partition_node = partition_linked_list(head); NODE *second_half = partition_node->next; partition_node->next = NULL; partition_node = second_half; head = sort_linked_list(head); partition_node = sort_linked_list(partition_node); head = merge_sorted_linked_lists(head,partition_node); return head; }
/*! Tests if the insertion sort algorithm generates the correct result. * @param list The array of values to make a linked list out of. * @param expected The array of values expected of the sorted output. * @param size The number of elements in the list. * @return true if the result is as expected, false otherwise. */ bool test_sort(int *list, int *expected, int size) { assert(size > 0); assert(list != NULL); assert(expected != NULL); // First we create a linked list out of the input array. Node *linked_list = malloc(sizeof(Node)); linked_list->value = list[0]; linked_list->next = NULL; Node *current_node = linked_list; for (int i = 1; i < size; i++) { Node *new_node = malloc(sizeof(Node)); new_node->value = list[i]; new_node->next = NULL; current_node->next = new_node; current_node = new_node; } // Now we sort the linked list. sort_linked_list(&linked_list); // Now iterate over the expected array and the linked list, and check values. bool result = true; current_node = linked_list; for (int i = 0; i < size; i++) { if (current_node->value != expected[i]) { result = false; break; } else { current_node = current_node->next; } } // Free the memory in the linked list. current_node = linked_list; while (current_node != NULL) { Node *temp = current_node; current_node = current_node->next; free(temp); } return result; }
void test_sort_linked_list() { int input_array[8][10] = {{9, 8, 7, 6, 5, 4, 3, 2, 1},{34, 76, 23, 98, 43},{2,1},{1},{NULL},{8, 7, 6, 5, 4, 3, 2, 1},{3,2,1},{236,235,440,100}}; int output_array[8][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9},{23, 34, 43, 76, 98},{1,2},{1},{NULL},{1, 2, 3, 4, 5, 6, 7, 8},{1,2,3},{100, 235, 236, 440}}; int iter_loop; for(iter_loop=0;iter_loop<8;iter_loop++) { NODE *input_list = create_linked_list(input_array[iter_loop]); NODE *output_list = create_linked_list(output_array[iter_loop]); input_list = sort_linked_list(input_list); NODE *head_input_list = input_list; NODE *head_output_list = output_list; if(input_list == NULL && output_list == NULL) { printf("ACCEPTED\n"); break; } while (input_list!=NULL || output_list!=NULL) { if(input_list->data != output_list->data) { printf("REJECTED\n"); delete_linked_list(head_input_list); delete_linked_list(head_output_list); break; } input_list = input_list->next; output_list = output_list->next; } if(input_list == NULL && output_list == NULL) { printf("ACCEPTED\n"); delete_linked_list(head_input_list); delete_linked_list(head_output_list); } } }
static void generate_xml_for_component(char *component_name, char *component_base) { PAPI_INFO api_info; char canonical_base[MAX_PATH]; char buf[200]; int complete; int implemented_total; int unimplemented_total; // Sort list api_info_list = sort_linked_list(api_info_list, 0, compare_api_order); implemented_total = 0; unimplemented_total = 0; api_info = api_info_list; while (api_info != NULL) { if (api_info->tag_id == TAG_IMPLEMENTED) implemented_total ++; else if (api_info->tag_id == TAG_UNIMPLEMENTED) unimplemented_total ++; api_info = api_info->next; } if (implemented_total + unimplemented_total > 0) complete = ((implemented_total) * 100) / (implemented_total + unimplemented_total); else complete = 100; strcpy(canonical_base, component_base); path_to_url(canonical_base); sprintf(buf, "<component name=\"%s\" base=\"%s\" complete=\"%d\" implemented_total=\"%d\" unimplemented_total=\"%d\">", component_name, canonical_base, complete, implemented_total, unimplemented_total); write_line(buf); if (api_info_list != NULL) { write_line("<functions>"); api_info = api_info_list; while (api_info != NULL) { sprintf(buf, "<f n=\"%s\" i=\"%s\" f=\"%s\" />", api_info->name, api_info->tag_id == TAG_IMPLEMENTED ? "true" : "false", get_filename_without_base(component_base, api_info->filename)); write_line(buf); api_info = api_info->next; } write_line("</functions>"); } write_line("</component>"); }
/** * Go through the freelists and free puddles with no used chunks. * * @return * Number of freed puddles. */ static size_t mempool_free_puddles (mempool_struct *pool) { size_t chunksize_real, nrof_arrays, i, j, freed; mempool_chunk_struct *last_free, *chunk; mempool_puddle_struct *puddle, *next_puddle; HARD_ASSERT(pool != NULL); if (pool->flags & MEMPOOL_BYPASS_POOLS) { return 0; } freed = 0; for (i = 0; i < MEMPOOL_NROF_FREELISTS; i++) { chunksize_real = sizeof(mempool_chunk_struct) + (pool->chunksize << i); nrof_arrays = pool->expand_size >> i; /* Free empty puddles and setup puddle-local freelists */ for (puddle = pool->puddlelist[i], pool->puddlelist[i] = NULL; puddle != NULL; puddle = next_puddle) { next_puddle = puddle->next; /* Count free chunks in puddle, and set up a local freelist */ puddle->first_free = puddle->last_free = NULL; puddle->nrof_free = 0; for (j = 0; j < nrof_arrays; j++) { chunk = (mempool_chunk_struct *) (((char *) puddle->first_chunk) + chunksize_real * j); /* Find free chunks. */ if (CHUNK_FREE(MEM_USERDATA(chunk))) { if (puddle->nrof_free == 0) { puddle->first_free = chunk; puddle->last_free = chunk; chunk->next = NULL; } else { chunk->next = puddle->first_free; puddle->first_free = chunk; } puddle->nrof_free++; } } /* Can we actually free this puddle? */ if (puddle->nrof_free == nrof_arrays || (deiniting && pool == pool_puddle)) { /* Yup. Forget about it. */ efree(puddle->first_chunk); if (!deiniting || pool != pool_puddle) { mempool_return(pool_puddle, puddle); } pool->nrof_free[i] -= nrof_arrays; pool->nrof_allocated[i] -= nrof_arrays; freed++; } else { /* Nope, keep this puddle: put it back into the tracking list */ puddle->next = pool->puddlelist[i]; pool->puddlelist[i] = puddle; } } /* Sort the puddles by amount of free chunks. It will let us set up the * freelist so that the chunks from the fullest puddles are used first. * This should (hopefully) help us free some of the lesser-used puddles * earlier. */ pool->puddlelist[i] = sort_linked_list(pool->puddlelist[i], 0, sort_puddle_by_nrof_free, NULL, NULL, NULL); /* Finally: restore the global freelist */ pool->freelist[i] = &end_marker; last_free = &end_marker; for (puddle = pool->puddlelist[i]; puddle != NULL; puddle = puddle->next) { if (puddle->nrof_free > 0) { if (pool->freelist[i] == &end_marker) { pool->freelist[i] = puddle->first_free; } else { last_free->next = puddle->first_free; } puddle->last_free->next = &end_marker; last_free = puddle->last_free; } } } return freed; }