This commit is contained in:
kr820js 2024-03-10 18:42:57 +01:00
parent 2deb1d262e
commit 646809b52e

View File

@ -16,58 +16,81 @@ list_t *append_list(list_t *list1, list_t *list2){
list3->length = list1->length+list2->length; list3->length = list1->length+list2->length;
//list->elements = list + sizeof(list_t); //list->elements = list + sizeof(list_t);
memcpy(list3->elements,list1->elements, list1->length*sizeof(list_element_t)); memcpy(list3->elements,list1->elements, list1->length*sizeof(list_element_t));
memcpy(list3->elements+list1->length*sizeof(list_element_t),list2->elements, list2->length*sizeof(list_element_t)); memcpy(list3->elements+list1->length,list2->elements, list2->length*sizeof(list_element_t));
return list3; return list3;
} }
// filter list returning only values that satisfy the filter function // filter list returning only values that satisfy the filter function
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
if(filter){}; for(size_t i=0; i<list->length; i++){
if(list){}; if(!filter(list->elements[i])){
return NULL; for (size_t j = i; j < list->length - 1; j++) {
list->elements[j] = list->elements[j + 1];
}
list->length=list->length-1;
}
}
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length;
memcpy(list_new->elements,list->elements, list->length*sizeof(list_element_t));
// list_t *result = (list_t*)malloc(sizeof(list_t) + 10 * sizeof(list_element_t));
return list_new;
} }
// returns the length of the list // returns the length of the list
size_t length_list(list_t *list){ size_t length_list(list_t *list){
if(list){};
return 0; return list->length;
} }
// return a list of elements whose values equal the list value transformed by // return a list of elements whose values equal the list value transformed by
// the mapping function // the mapping function
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){ list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){
if(map){}; for(size_t i=0; i<list->length; i++){
if(list){}; list->elements[i]=map(list->elements[i]);
return NULL; }
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length;
memcpy(list_new->elements,list->elements, list->length*sizeof(list_element_t));
return list_new;
} }
// folds (reduces) the given list from the left with a function // 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 foldl_list(list_t *list, list_element_t initial,list_element_t (*foldl)(list_element_t,list_element_t)){
list_element_t fold_step=initial;
if(list||foldl){}; for(size_t i=0; i<list->length; i++){
if(initial){}; fold_step=foldl(fold_step,list->elements[i]);
list_element_t res=0; }
return res; return fold_step;
} }
// folds (reduces) the given list from the right with a function // 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 foldr_list(list_t *list, list_element_t initial,list_element_t (*foldr)(list_element_t,list_element_t)){
if(list){}; list_element_t fold_step=initial;
if(initial||foldr){}; if(list->length>0){
list_element_t res=0; for(size_t i=list->length-1; i!=0; i--){
return res; fold_step=foldr(list->elements[i],fold_step);
}
fold_step=foldr(list->elements[0],fold_step);
}
return fold_step;
} }
// reverse the elements of the list // reverse the elements of the list
list_t *reverse_list(list_t *list){ list_t *reverse_list(list_t *list){
if(list){}; list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
return NULL; list_new->length=list->length;
size_t i,j;
for( i=list->length-1 , j=0 ; j<list->length; j++, i--){
list_new->elements[i]=list->elements[j];
}
return list_new;
} }