103 lines
2.8 KiB
C
103 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/>.
|
|
*/
|
|
|
|
#ifndef _SINGLY_LINKED_LIST_H_
|
|
#define _SINGLY_LINKED_LIST_H_
|
|
|
|
#include <stdint.h>
|
|
|
|
typedef struct singly_linked_list SlList;
|
|
|
|
struct singly_linked_list {
|
|
void *data; /**< @brief Pointer to the data */
|
|
SlList *next; /**< @brief Pointer to next element */
|
|
};
|
|
|
|
#define sl_list_next(sll) ((sll)->next)
|
|
|
|
/**
|
|
* @brief Append element to list
|
|
* @param list List to append to. May be NULL
|
|
* @param data data to stor ein the element
|
|
* @return New Head of list. Store this.
|
|
*/
|
|
SlList *sl_list_append(SlList *list, void *data);
|
|
|
|
/**
|
|
* @brief Prepend an element to list
|
|
* @param list List to prepend data. May be NULL
|
|
* @param data Data to store
|
|
* @return New head of list. Store this
|
|
*/
|
|
SlList *sl_list_prepend(SlList *list, void *data);
|
|
|
|
/**
|
|
* @brief Insert element into list at position.
|
|
*
|
|
* If the position is larger than the list's size,
|
|
* the element is appended at the end.
|
|
*
|
|
* @param list List to insert
|
|
* @param position Position (0 based. 0 equals head)
|
|
* @param data Data to append
|
|
* @return New list head
|
|
*/
|
|
SlList *sl_list_insert(SlList *list, uint32_t position, void *data);
|
|
|
|
/**
|
|
* @brief Remove an elemnt from the list
|
|
*
|
|
* If the elemnt is not found, nothing is removed.
|
|
* If multiple elments contain this datum, only the first is rmeoved.
|
|
*
|
|
* @param list List ot remove from
|
|
* @param data Pointer to remove.
|
|
* @return New list head
|
|
*/
|
|
SlList *sl_list_remove(SlList *list, const void *data);
|
|
|
|
/**
|
|
* @brief Free a SlList
|
|
* @param list List to free
|
|
* @warning This function does not deallocate the data stored. Use @ref sl_list_free_full for that.
|
|
*/
|
|
void sl_list_free(SlList *list);
|
|
|
|
/**
|
|
* @brief Free a list including its data items
|
|
* @param list List to fully free
|
|
* @param destroy_element Destruction function for the data items
|
|
*/
|
|
void sl_list_free_full(SlList *list, void (*destroy_element)(void *));
|
|
|
|
/**
|
|
* @brief Get Length of list
|
|
* @param list List
|
|
* @return Length of list
|
|
*/
|
|
uint32_t sl_list_length(const SlList *list);
|
|
|
|
/**
|
|
* @brief Get nth element in list.
|
|
* @param list List
|
|
* @param n Position of element
|
|
* @return nth Element or NULL in case list is to short.
|
|
*/
|
|
SlList *sl_list_nth(SlList *list, uint32_t n);
|
|
|
|
#endif /* _SINGLY_LINKED_LIST_H_ */
|