2024-03-10 14:35:01 +00:00
|
|
|
#include "list_ops.h"
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
#include<string.h>
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
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;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
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;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
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;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
size_t length_list(list_t* list) {
|
|
|
|
size_t count = 0;
|
|
|
|
list_t* current = list;
|
|
|
|
while (current != NULL) {
|
|
|
|
count++;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return count;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
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;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
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;
|
2024-03-10 14:35:01 +00:00
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
while (current != NULL) {
|
|
|
|
result = foldl(result, current->data);
|
|
|
|
current = current->next;
|
|
|
|
}
|
2024-03-10 14:35:01 +00:00
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
return result;
|
2024-03-10 14:35:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
list_t* reverse_list(list_t* list) {
|
|
|
|
list_t* prev = NULL;
|
|
|
|
list_t* current = list;
|
|
|
|
list_t* next;
|
2024-03-10 14:35:01 +00:00
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
while (current != NULL) {
|
|
|
|
next = current->next;
|
|
|
|
current->next = prev;
|
|
|
|
prev = current;
|
|
|
|
current = next;
|
|
|
|
}
|
2024-03-10 14:35:01 +00:00
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
return prev;
|
|
|
|
}
|
2024-03-10 14:35:01 +00:00
|
|
|
|
2024-03-10 15:07:14 +00:00
|
|
|
void delete_list(list_t* list) {
|
|
|
|
while (list != NULL) {
|
|
|
|
list_t* temp = list;
|
|
|
|
list = list->next;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
}
|