139 lines
3.2 KiB
C
139 lines
3.2 KiB
C
#include "list_ops.h"
|
|
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
#include<string.h>
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|