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

View File

@ -1,138 +1,21 @@
#include "list_ops.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <stdlib.h>
#include <stdbool.h>
list_t* new_list(size_t length, list_element_t elements[]) {
list_t* list = NULL;
list_t* current = NULL;
list_t *new_list(size_t length, list_element_t elements[]) {
size_t size = sizeof(list_t) + length * sizeof(list_element_t);
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;
list_t *newList = (list_t *)malloc(size);
if (list == NULL) {
list = new_node;
current = list;
} else {
current->next = new_node;
current = current->next;
}
if (newList == NULL) {
return NULL;
}
return list;
}
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;
}
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;
}
size_t length_list(list_t* list) {
size_t count = 0;
list_t* current = list;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
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;
}
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;
}
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;
}
void delete_list(list_t* list) {
while (list != NULL) {
list_t* temp = list;
list = list->next;
free(temp);
}
newList->length = length;
for (size_t i = 0; i < length; ++i) {
newList->elements[i] = elements[i];
}
return newList;
}