From fb400dc1684783806d3ee04c1f1f2d287d5a4f6c Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Sun, 10 Mar 2024 22:56:05 +0100 Subject: [PATCH] add linked list searching and the second test --- lab01/zad2/include/linked_list.h | 8 +++++ lab01/zad2/src/linked_list.c | 20 ++++++++++++ lab01/zad2/src/main.c | 53 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/lab01/zad2/include/linked_list.h b/lab01/zad2/include/linked_list.h index b91c4d6..b0658e2 100644 --- a/lab01/zad2/include/linked_list.h +++ b/lab01/zad2/include/linked_list.h @@ -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 diff --git a/lab01/zad2/src/linked_list.c b/lab01/zad2/src/linked_list.c index af9a423..498d234 100644 --- a/lab01/zad2/src/linked_list.c +++ b/lab01/zad2/src/linked_list.c @@ -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; +} diff --git a/lab01/zad2/src/main.c b/lab01/zad2/src/main.c index 70b7981..0e895af 100644 --- a/lab01/zad2/src/main.c +++ b/lab01/zad2/src/main.c @@ -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; }