This commit is contained in:
Michal Utľák 2024-03-10 16:07:14 +01:00
parent c3a7ed4b02
commit 0dc835c900

View File

@ -3,63 +3,136 @@
#include<stdlib.h>
#include<string.h>
list_t *new_list(size_t length, list_element_t elements[]){
list_t* list = (list_t*)malloc(sizeof(list_t) + length * sizeof(list_element_t));
list->length = length;
memcpy(list->elements,elements, length*sizeof(list_element_t));
return list;
list_t* new_list(size_t length, list_element_t elements[]) {
list_t* list = NULL;
list_t* current = NULL;
for (size_t i = 0; i < length; i++) {
list_t* new_node = (list_t*)malloc(sizeof(list_t));
new_node->data = elements[i];
new_node->next = NULL;
if (list == NULL) {
list = new_node;
current = list;
} else {
current->next = new_node;
current = current->next;
}
}
return list;
}
list_t *append_list(list_t *list1, list_t *list2){
return NULL;
list_t* append_list(list_t* list1, list_t* list2) {
if (list1 == NULL) {
return list2;
}
if (list2 == NULL) {
return list1;
}
list_t* current = list1;
while (current->next != NULL) {
current = current->next;
}
current->next = list2;
return list1;
}
// filter list returning only values that satisfy the filter function
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
return NULL;
list_t* filter_list(list_t* list, bool (*filter)(list_element_t)) {
list_t* filtered = NULL;
list_t* current = list;
list_t* filtered_current = NULL;
while (current != NULL) {
if (filter(current->data)) {
list_t* new_node = (list_t*)malloc(sizeof(list_t));
new_node->data = current->data;
new_node->next = NULL;
if (filtered == NULL) {
filtered = new_node;
filtered_current = filtered;
} else {
filtered_current->next = new_node;
filtered_current = filtered_current->next;
}
}
current = current->next;
}
return filtered;
}
// returns the length of the list
size_t length_list(list_t *list){
return 0;
size_t length_list(list_t* list) {
size_t count = 0;
list_t* current = list;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
// return a list of elements whose values equal the list value transformed by
// the mapping function
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){
return NULL;
list_t* map_list(list_t* list, list_element_t (*map)(list_element_t)) {
list_t* mapped = NULL;
list_t* current = list;
list_t* mapped_current = NULL;
while (current != NULL) {
list_element_t mapped_value = map(current->data);
list_t* new_node = (list_t*)malloc(sizeof(list_t));
new_node->data = mapped_value;
new_node->next = NULL;
if (mapped == NULL) {
mapped = new_node;
mapped_current = mapped;
} else {
mapped_current->next = new_node;
mapped_current = mapped_current->next;
}
current = current->next;
}
return mapped;
}
// folds (reduces) the given list from the left with a function
list_element_t foldl_list(list_t *list, list_element_t initial,
list_element_t (*foldl)(list_element_t,
list_element_t)){
list_element_t res=0;
return res;
list_element_t foldl_list(list_t* list, list_element_t initial,
list_element_t (*foldl)(list_element_t, list_element_t)) {
list_element_t result = initial;
list_t* current = list;
while (current != NULL) {
result = foldl(result, current->data);
current = current->next;
}
return result;
}
// folds (reduces) the given list from the right with a function
list_element_t foldr_list(list_t *list, list_element_t initial,
list_element_t (*foldr)(list_element_t,
list_element_t)){
list_element_t res=0;
return res;
list_t* reverse_list(list_t* list) {
list_t* prev = NULL;
list_t* current = list;
list_t* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// reverse the elements of the list
list_t *reverse_list(list_t *list){
return NULL;
}
// destroy the entire list
// list will be a dangling pointer after calling this method on it
void delete_list(list_t *list){
void delete_list(list_t* list) {
while (list != NULL) {
list_t* temp = list;
list = list->next;
free(temp);
}
}