add linked list searching and the second test
This commit is contained in:
parent
4245731593
commit
fb400dc168
3 changed files with 81 additions and 0 deletions
|
@ -15,10 +15,18 @@ typedef struct {
|
|||
Node *current;
|
||||
} LinkedList;
|
||||
|
||||
typedef struct {
|
||||
bool success;
|
||||
size_t index;
|
||||
size_t comparisons;
|
||||
} SearchResult;
|
||||
|
||||
LinkedList linked_list_new(size_t element_size);
|
||||
|
||||
void linked_list_insert(LinkedList *l, void *value);
|
||||
|
||||
LinkedList linked_list_merge(LinkedList l1, LinkedList l2);
|
||||
|
||||
SearchResult linked_list_search(LinkedList l, void *value, int (*compare_values)(void *v1, void *v2));
|
||||
|
||||
#endif // _KIK_ZAD2_LINKED_LIST_H
|
||||
|
|
|
@ -60,3 +60,23 @@ LinkedList linked_list_merge(LinkedList l1, LinkedList l2) {
|
|||
|
||||
return l1;
|
||||
}
|
||||
|
||||
SearchResult linked_list_search(LinkedList l, void *value, int (*compare_values)(void *v1, void *v2)) {
|
||||
SearchResult result;
|
||||
result.comparisons = 0;
|
||||
result.index = 0;
|
||||
result.success = false;
|
||||
|
||||
Node *current = l.head;
|
||||
do {
|
||||
result.comparisons += 1;
|
||||
if (compare_values(value, current->value) == 0) {
|
||||
result.success = true;
|
||||
break;
|
||||
}
|
||||
result.index += 1;
|
||||
current = current->next;
|
||||
} while (current != l.head);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -32,10 +32,63 @@ void merge_test(void) {
|
|||
} while (l_node != l.head);
|
||||
}
|
||||
|
||||
int compare_uint64_t(void *v1, void *v2) {
|
||||
uint64_t u1 = *(uint64_t*)v1;
|
||||
uint64_t u2 = *(uint64_t*)v2;
|
||||
|
||||
if (u1 > u2) {
|
||||
return 1;
|
||||
} else if (u1 < u2) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t T_LENGTH = 10000;
|
||||
const uint64_t MAX_NUM = 100000;
|
||||
const size_t TEST_ITERATIONS = 1000;
|
||||
|
||||
void average_cost_test(void) {
|
||||
uint64_t T[T_LENGTH];
|
||||
LinkedList L = linked_list_new(sizeof(uint64_t));
|
||||
|
||||
for (size_t i = 0; i < T_LENGTH; ++i) {
|
||||
T[i] = (uint64_t)rand() % (MAX_NUM + 1);
|
||||
linked_list_insert(&L, &T[i]);
|
||||
}
|
||||
|
||||
// searching for randomly chosen elements from T
|
||||
double average_T_cost = 0.0;
|
||||
// searching for randomly chosen numbers [0, 100000]
|
||||
double average_rand_cost = 0.0;
|
||||
|
||||
for (size_t i = 0; i < TEST_ITERATIONS; ++i) {
|
||||
uint64_t t = T[(unsigned long)rand() % T_LENGTH];
|
||||
uint64_t rand_num = (uint64_t)rand() % (MAX_NUM + 1);
|
||||
|
||||
SearchResult t_result = linked_list_search(L, &t, &compare_uint64_t);
|
||||
SearchResult rand_result = linked_list_search(L, &rand_num, &compare_uint64_t);
|
||||
|
||||
average_T_cost += (double)t_result.comparisons;
|
||||
average_rand_cost += (double)rand_result.comparisons;
|
||||
}
|
||||
|
||||
average_T_cost /= (double)TEST_ITERATIONS;
|
||||
average_rand_cost /= (double)TEST_ITERATIONS;
|
||||
|
||||
printf("average search cost for randomly picked elements from T: %lf\n", average_T_cost);
|
||||
printf("average search cost for randomly picked numbers from 0 to %ld: %lf\n", MAX_NUM, average_rand_cost);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
merge_test();
|
||||
|
||||
printf("\n\n\n\n\n");
|
||||
|
||||
average_cost_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue