2024-03-10 15:16:55 +00:00
|
|
|
#include "list_ops.h"
|
2024-03-10 15:32:35 +00:00
|
|
|
#include <string.h>
|
2024-03-10 15:16:55 +00:00
|
|
|
// constructs a new list
|
|
|
|
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;
|
2024-03-10 16:37:21 +00:00
|
|
|
//list->elements = list + sizeof(list_t);
|
2024-03-10 15:16:55 +00:00
|
|
|
memcpy(list->elements,elements, length*sizeof(list_element_t));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append entries to a list and return the new list
|
|
|
|
list_t *append_list(list_t *list1, list_t *list2){
|
2024-03-10 16:37:21 +00:00
|
|
|
// list3.lenght=list1->lenght+list2->lenght;
|
|
|
|
list_t* list3 = (list_t*)malloc(sizeof(list_t) + (list1->length+list2->length) * sizeof(list_element_t));
|
|
|
|
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->length*sizeof(list_element_t),list2->elements, list2->length*sizeof(list_element_t));
|
2024-03-10 15:16:55 +00:00
|
|
|
|
2024-03-10 16:37:21 +00:00
|
|
|
return list3;
|
2024-03-10 15:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// filter list returning only values that satisfy the filter function
|
|
|
|
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){
|
2024-03-10 15:32:35 +00:00
|
|
|
if(filter){};
|
|
|
|
if(list){};
|
2024-03-10 15:16:55 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the length of the list
|
|
|
|
size_t length_list(list_t *list){
|
2024-03-10 15:32:35 +00:00
|
|
|
if(list){};
|
2024-03-10 15:16:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)){
|
2024-03-10 15:32:35 +00:00
|
|
|
if(map){};
|
|
|
|
if(list){};
|
2024-03-10 15:16:55 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// folds (reduces) the given list from the left with a function
|
2024-03-10 15:32:35 +00:00
|
|
|
list_element_t foldl_list(list_t *list, list_element_t initial,list_element_t (*foldl)(list_element_t,list_element_t)){
|
|
|
|
|
|
|
|
if(list||foldl){};
|
|
|
|
if(initial){};
|
2024-03-10 15:16:55 +00:00
|
|
|
list_element_t res=0;
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// folds (reduces) the given list from the right with a function
|
2024-03-10 15:32:35 +00:00
|
|
|
list_element_t foldr_list(list_t *list, list_element_t initial,list_element_t (*foldr)(list_element_t,list_element_t)){
|
|
|
|
if(list){};
|
|
|
|
if(initial||foldr){};
|
2024-03-10 15:16:55 +00:00
|
|
|
list_element_t res=0;
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse the elements of the list
|
|
|
|
list_t *reverse_list(list_t *list){
|
2024-03-10 15:32:35 +00:00
|
|
|
if(list){};
|
2024-03-10 15:16:55 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy the entire list
|
|
|
|
// list will be a dangling pointer after calling this method on it
|
|
|
|
void delete_list(list_t *list){
|
|
|
|
free(list);
|
|
|
|
}
|