pls
This commit is contained in:
parent
87e1e23e2f
commit
c910519200
@ -5,19 +5,19 @@
|
||||
list_t *new_list(size_t length, list_element_t elements[]) {
|
||||
size_t size = sizeof(list_t) + length * sizeof(list_element_t);
|
||||
|
||||
list_t *newList = (list_t *)malloc(size);
|
||||
list_t *resultList = (list_t *)malloc(size);
|
||||
|
||||
if (newList == NULL) {
|
||||
if (resultList == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newList->length = length;
|
||||
resultList->length = length;
|
||||
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
newList->elements[i] = elements[i];
|
||||
resultList->elements[i] = elements[i];
|
||||
}
|
||||
|
||||
return newList;
|
||||
return resultList;
|
||||
}
|
||||
|
||||
list_t *append_list(list_t *list1, list_t *list2) {
|
||||
@ -31,23 +31,23 @@ list_t *append_list(list_t *list1, list_t *list2) {
|
||||
|
||||
size_t totalSize = sizeof(list_t) + (list1->length + list2->length) * sizeof(list_element_t);
|
||||
|
||||
list_t *newList = (list_t *)malloc(totalSize);
|
||||
list_t *resultList = (list_t *)malloc(totalSize);
|
||||
|
||||
if (!newList) {
|
||||
if (!resultList) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newList->length = list1->length + list2->length;
|
||||
resultList->length = list1->length + list2->length;
|
||||
|
||||
for (size_t i = 0; i < list1->length; ++i) {
|
||||
newList->elements[i] = list1->elements[i];
|
||||
resultList->elements[i] = list1->elements[i];
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < list2->length; ++i) {
|
||||
newList->elements[list1->length + i] = list2->elements[i];
|
||||
resultList->elements[list1->length + i] = list2->elements[i];
|
||||
}
|
||||
|
||||
return newList;
|
||||
return resultList;
|
||||
}
|
||||
|
||||
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
|
||||
@ -67,23 +67,23 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
|
||||
}
|
||||
|
||||
size_t totalSize = sizeof(list_t) + count * sizeof(list_element_t);
|
||||
list_t *filtered_list = (list_t *)malloc(totalSize);
|
||||
list_t *filteredResult = (list_t *)malloc(totalSize);
|
||||
|
||||
if (!filtered_list) {
|
||||
if (!filteredResult) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
filtered_list->length = count;
|
||||
filteredResult->length = count;
|
||||
|
||||
size_t filteredIndex = 0;
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
if (filter(list->elements[i])) {
|
||||
filtered_list->elements[filteredIndex] = list->elements[i];
|
||||
filteredResult->elements[filteredIndex] = list->elements[i];
|
||||
filteredIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
return filtered_list;
|
||||
return filteredResult;
|
||||
}
|
||||
|
||||
size_t length_list(list_t *list) {
|
||||
@ -99,17 +99,17 @@ list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_t *mapped_list = new_list(list->length, NULL);
|
||||
list_t *mappedResult = new_list(list->length, NULL);
|
||||
|
||||
if (mapped_list == NULL) {
|
||||
if (mappedResult == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
mapped_list->elements[i] = map(list->elements[i]);
|
||||
mappedResult->elements[i] = map(list->elements[i]);
|
||||
}
|
||||
|
||||
return mapped_list;
|
||||
return mappedResult;
|
||||
}
|
||||
|
||||
|
||||
@ -120,13 +120,13 @@ list_element_t foldl_list(list_t *list, list_element_t initial,
|
||||
return initial;
|
||||
}
|
||||
|
||||
list_element_t accumulator = initial;
|
||||
list_element_t result = initial;
|
||||
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
accumulator = foldl(accumulator, list->elements[i]);
|
||||
result = foldl(result, list->elements[i]);
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
return result;
|
||||
}
|
||||
|
||||
list_element_t foldr_list(list_t *list, list_element_t initial,
|
||||
@ -136,13 +136,13 @@ list_element_t foldr_list(list_t *list, list_element_t initial,
|
||||
return initial;
|
||||
}
|
||||
|
||||
list_element_t result = initial;
|
||||
list_element_t foldResult = initial;
|
||||
|
||||
for (size_t i = 0; i < list->length; ++i) {
|
||||
result = foldr(list->elements[list->length - 1 - i], result);
|
||||
foldResult = foldr(list->elements[list->length - 1 - i], foldResult);
|
||||
}
|
||||
|
||||
return result;
|
||||
return foldResult;
|
||||
}
|
||||
|
||||
list_t *reverse_list(list_t *list) {
|
||||
@ -150,17 +150,17 @@ list_t *reverse_list(list_t *list) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_t *reversed_list = new_list(list->length, NULL);
|
||||
list_t *reversedResult = new_list(list->length, NULL);
|
||||
|
||||
if (!reversed_list) {
|
||||
if (!reversedResult) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = list->length; i > 0; --i) {
|
||||
reversed_list->elements[list->length - i] = list->elements[i - 1];
|
||||
reversedResult->elements[list->length - i] = list->elements[i - 1];
|
||||
}
|
||||
|
||||
return reversed_list;
|
||||
return reversedResult;
|
||||
}
|
||||
|
||||
void delete_list(list_t *list) {
|
||||
|
Loading…
Reference in New Issue
Block a user