From 7a34b3d412f5022d4ce25d84ec7fb4002b1fb5db Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Sat, 21 Feb 2026 17:33:28 +0100 Subject: Implement libft Part 3 (linked lists) with tests Add t_list struct and 9 linked list functions: ft_lstnew, ft_lstadd_front, ft_lstsize, ft_lstlast, ft_lstadd_back, ft_lstdelone, ft_lstclear, ft_lstiter, ft_lstmap. --- inc/libft.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'inc/libft.h') diff --git a/inc/libft.h b/inc/libft.h index 05a5ae9..48b2c0b 100644 --- a/inc/libft.h +++ b/inc/libft.h @@ -135,4 +135,33 @@ void ft_putendl_fd (char *s, int fd); /** @brief Write integer @p n in decimal to @p fd. */ void ft_putnbr_fd (int n, int fd); +/* ====================================== + * Linked list + * ====================================== */ + +typedef struct s_list +{ + void *content; /**< Node payload (owned by the caller). */ + struct s_list *next; /**< Next node, or NULL for the tail. */ +} t_list; + +/** @brief Allocate a new list node with @p content; next is NULL. */ +t_list *ft_lstnew (void *content); +/** @brief Prepend @p new to the list pointed to by @p lst. */ +void ft_lstadd_front (t_list **lst, t_list *new); +/** @brief Count the number of nodes in @p lst. */ +int ft_lstsize (t_list *lst); +/** @brief Return the last node of @p lst. */ +t_list *ft_lstlast (t_list *lst); +/** @brief Append @p new to the end of the list pointed to by @p lst. */ +void ft_lstadd_back (t_list **lst, t_list *new); +/** @brief Delete one node's content with @p del and free the node. */ +void ft_lstdelone (t_list *lst, void (*del) (void *)); +/** @brief Delete and free all nodes in @p lst using @p del; set *lst=NULL. */ +void ft_lstclear (t_list **lst, void (*del) (void *)); +/** @brief Apply @p f to the content of each node in @p lst. */ +void ft_lstiter (t_list *lst, void (*f) (void *)); +/** @brief Map @p f over @p lst, creating a new list; use @p del on failure. */ +t_list *ft_lstmap (t_list *lst, void *(*f) (void *), void (*del) (void *)); + #endif -- cgit v1.2.3