rerererrr

This commit is contained in:
Michal Utľák 2024-03-10 19:23:07 +01:00
parent 9ca763cbb8
commit 1e99382017

View File

@ -38,11 +38,6 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
return NULL; return NULL;
} }
list_t *filteredList = (list_t *)malloc(sizeof(list_t));
if (filteredList == NULL) {
return NULL;
}
size_t count = 0; size_t count = 0;
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
if (filter(list->elements[i])) { if (filter(list->elements[i])) {
@ -50,16 +45,14 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
} }
} }
filteredList->length = count; list_t *filteredList = (list_t *)malloc(sizeof(list_t) + count * sizeof(list_element_t));
if (filteredList == NULL) {
filteredList->elements = (list_element_t *)malloc(count * sizeof(list_element_t));
if (filteredList->elements == NULL) {
free(filteredList);
return NULL; return NULL;
} }
size_t filteredIndex = 0; filteredList->length = count;
for (size_t i = 0; i < list->length; ++i) {
for (size_t i = 0, filteredIndex = 0; i < list->length; ++i) {
if (filter(list->elements[i])) { if (filter(list->elements[i])) {
filteredList->elements[filteredIndex++] = list->elements[i]; filteredList->elements[filteredIndex++] = list->elements[i];
} }
@ -87,12 +80,6 @@ list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
mappedList->length = list->length; mappedList->length = list->length;
mappedList->elements = (list_element_t *)malloc(list->length * sizeof(list_element_t));
if (mappedList->elements == NULL) {
free(mappedList);
return NULL;
}
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
mappedList->elements[i] = map(list->elements[i]); mappedList->elements[i] = map(list->elements[i]);
} }
@ -140,12 +127,6 @@ list_t *reverse_list(list_t *list) {
reversedList->length = list->length; reversedList->length = list->length;
reversedList->elements = (list_element_t *)malloc(list->length * sizeof(list_element_t));
if (reversedList->elements == NULL) {
free(reversedList);
return NULL;
}
for (size_t i = 0; i < list->length; ++i) { for (size_t i = 0; i < list->length; ++i) {
reversedList->elements[i] = list->elements[list->length - i - 1]; reversedList->elements[i] = list->elements[list->length - i - 1];
} }
@ -155,7 +136,6 @@ list_t *reverse_list(list_t *list) {
void delete_list(list_t *list) { void delete_list(list_t *list) {
if (list != NULL) { if (list != NULL) {
free(list->elements);
free(list); free(list);
} }
} }