144 lines
2.8 KiB
C
144 lines
2.8 KiB
C
/*
|
|
* This file is part of the linklist Library.
|
|
* Copyright (c) 2021 Mario Hüttel.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 2 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <linklist-lib/singly-linked-list.h>
|
|
#include <stdlib.h>
|
|
|
|
static SlList *sl_list_alloc()
|
|
{
|
|
return (SlList *)malloc(sizeof(SlList));
|
|
}
|
|
|
|
static void sl_list_dealloc(SlList *list)
|
|
{
|
|
if (list)
|
|
free(list);
|
|
}
|
|
|
|
SlList *sl_list_append(SlList *list, void *data)
|
|
{
|
|
SlList *new_element;
|
|
SlList **next_iter;
|
|
|
|
/* Allocate new element for data */
|
|
new_element = sl_list_alloc();
|
|
new_element->data = data;
|
|
new_element->next = NULL;
|
|
|
|
for (next_iter = &list; *next_iter; next_iter = &(*next_iter)->next);
|
|
|
|
*next_iter = new_element;
|
|
|
|
return list;
|
|
}
|
|
|
|
SlList *sl_list_prepend(SlList *list, void *data)
|
|
{
|
|
SlList *new_element;
|
|
|
|
/* Allocate new element for data */
|
|
new_element = sl_list_alloc();
|
|
new_element->data = data;
|
|
new_element->next = list;
|
|
|
|
return new_element;
|
|
}
|
|
|
|
SlList *sl_list_insert(SlList *list, uint32_t position, void *data)
|
|
{
|
|
uint32_t idx;
|
|
SlList *new_element;
|
|
SlList **next_iter;
|
|
|
|
new_element = sl_list_alloc();
|
|
new_element->data = data;
|
|
|
|
idx = 0;
|
|
for (next_iter = &list; next_iter; next_iter = &(*next_iter)->next) {
|
|
if (idx == position || !*next_iter) {
|
|
new_element->next = *next_iter;
|
|
*next_iter = new_element;
|
|
break;
|
|
}
|
|
idx++;
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
SlList *sl_list_remove(SlList *list, const void *data)
|
|
{
|
|
SlList **next_iter;
|
|
SlList *tmp;
|
|
|
|
for (next_iter = &list; next_iter; next_iter = &(*next_iter)->next) {
|
|
if (*next_iter == NULL)
|
|
break;
|
|
if ((*next_iter)->data == data) {
|
|
tmp = (*next_iter)->next;
|
|
sl_list_dealloc(*next_iter);
|
|
*next_iter = tmp;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
void sl_list_free(SlList *list)
|
|
{
|
|
sl_list_free_full(list, NULL);
|
|
}
|
|
|
|
void sl_list_free_full(SlList *list, void (*destroy_element)(void *))
|
|
{
|
|
SlList *next;
|
|
|
|
while (list) {
|
|
next = list->next;
|
|
if (destroy_element)
|
|
destroy_element(list->data);
|
|
sl_list_dealloc(list);
|
|
list = next;
|
|
}
|
|
}
|
|
|
|
uint32_t sl_list_length(const SlList *list)
|
|
{
|
|
uint32_t count = 0;
|
|
|
|
for (; list; list = sl_list_next(list))
|
|
count++;
|
|
|
|
return count;
|
|
}
|
|
|
|
SlList *sl_list_nth(SlList *list, uint32_t n)
|
|
{
|
|
uint32_t idx;
|
|
SlList *ret = NULL;
|
|
|
|
for (idx = 0; list; list = sl_list_next(list), idx++) {
|
|
if (n == idx) {
|
|
ret = list;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|