From 3f810c9a873d31b313fb18ef495c9cc7a7cc64f0 Mon Sep 17 00:00:00 2001 From: Denys Sanchuk Date: Tue, 8 Apr 2025 12:54:15 +0000 Subject: [PATCH] Aktualizovat du6/list_ops.c --- du6/list_ops.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/du6/list_ops.c b/du6/list_ops.c index 8bfcc8d..af74029 100644 --- a/du6/list_ops.c +++ b/du6/list_ops.c @@ -25,4 +25,25 @@ list_t *append_list(list_t *list1, list_t *list2) { memcpy(result->elements + len1, list2->elements, sizeof(list_element_t) * len2); return result; +} +list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) { + if (!list || list->length == 0) return new_list(0, NULL); + + list_element_t *temp = malloc(sizeof(list_element_t) * list->length); + if (!temp) return NULL; + + size_t count = 0; + for (size_t i = 0; i < list->length; ++i) { + if (filter(list->elements[i])) { + temp[count++] = list->elements[i]; + } + } + + list_t *result = new_list(count, temp); + free(temp); + return result; +} + +size_t length_list(list_t *list) { + return list ? list->length : 0; } \ No newline at end of file