final submit

This commit is contained in:
kr820js 2024-03-14 06:40:52 +01:00
parent 646809b52e
commit 951db72fdd

View File

@ -1,27 +1,21 @@
#include "list_ops.h" #include "list_ops.h"
#include <string.h> #include <string.h>
// constructs a new list
list_t *new_list(size_t length, list_element_t elements[]){ 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_t* list = (list_t*)malloc(sizeof(list_t) + length * sizeof(list_element_t));
list->length = length; list->length = length;
//list->elements = list + sizeof(list_t);
memcpy(list->elements,elements, length*sizeof(list_element_t)); memcpy(list->elements,elements, length*sizeof(list_element_t));
return list; return list;
} }
// append entries to a list and return the new list
list_t *append_list(list_t *list1, list_t *list2){ list_t *append_list(list_t *list1, list_t *list2){
// list3.lenght=list1->lenght+list2->lenght;
list_t* list3 = (list_t*)malloc(sizeof(list_t) + (list1->length+list2->length) * sizeof(list_element_t)); list_t* list3 = (list_t*)malloc(sizeof(list_t) + (list1->length+list2->length) * sizeof(list_element_t));
list3->length = list1->length+list2->length; list3->length = list1->length+list2->length;
//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,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
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
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])){
@ -34,20 +28,16 @@ list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t)); list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length; list_new->length=list->length;
memcpy(list_new->elements,list->elements, list->length*sizeof(list_element_t)); 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; return list_new;
} }
// returns the length of the list
size_t length_list(list_t *list){ size_t length_list(list_t *list){
return list->length; return list->length;
} }
// 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)){ list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){
for(size_t i=0; i<list->length; i++){ for(size_t i=0; i<list->length; i++){
list->elements[i]=map(list->elements[i]); list->elements[i]=map(list->elements[i]);
@ -59,7 +49,6 @@ list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){
} }
// 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; list_element_t fold_step=initial;
for(size_t i=0; i<list->length; i++){ for(size_t i=0; i<list->length; i++){
@ -69,7 +58,6 @@ list_element_t foldl_list(list_t *list, list_element_t initial,list_element_t (*
} }
// 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)){
list_element_t fold_step=initial; list_element_t fold_step=initial;
if(list->length>0){ if(list->length>0){
@ -82,7 +70,6 @@ list_element_t foldr_list(list_t *list, list_element_t initial,list_element_t (*
} }
// reverse the elements of the list
list_t *reverse_list(list_t *list){ list_t *reverse_list(list_t *list){
list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t)); list_t *list_new = (list_t*)malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
list_new->length=list->length; list_new->length=list->length;
@ -94,8 +81,6 @@ list_t *reverse_list(list_t *list){
} }
// 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){
free(list); free(list);
} }