Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_lstmap.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_lstmap.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: raphalme <raphalme@student.42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/12/29 19:23:00 by raphalme #+# #+# */
9/* Updated: 2026/04/14 09:18:55 by raphalme ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "../includes/libft.h"
14#include "../includes/types.h"
15
26t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
27{
28 t_list *new_list;
29 t_list *new_elem;
30 void *content;
31
32 if (!lst || !f || !del)
33 return (NULL);
34 new_list = NULL;
35 while (lst)
36 {
37 content = f(lst->content);
38 new_elem = ft_lstnew(content);
39 if (!new_elem)
40 {
41 del(content);
42 ft_lstclear(&new_list, del);
43 return (NULL);
44 }
45 ft_lstadd_back(&new_list, new_elem);
46 lst = lst->next;
47 }
48 return (new_list);
49}
t_list * ft_lstmap(t_list *lst, void *(*f)(void *), void(*del)(void *))
Creates a new linked list with the results of applying a function to each element of the original lis...
Definition ft_lstmap.c:26
void ft_lstadd_back(t_list **lst, t_list *new)
Adds a new element to the end of a linked list. This function takes a pointer to the first element of...
void ft_lstclear(t_list **lst, void(*del)(void *))
Clears a linked list. This function takes a pointer to the first element of a linked list and a funct...
Definition ft_lstclear.c:25
t_list * ft_lstnew(void *content)
Creates a new linked-list node.
Definition ft_lstnew.c:24
Definition types.h:4
struct s_list * next
Definition types.h:6
void * content
Definition types.h:5