diff options
Diffstat (limited to 'libft/ft_lstmap.c')
-rw-r--r-- | libft/ft_lstmap.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100644 index 0000000..e962de6 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kdx <kdx @student.42angouleme.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/29 03:32:46 by kdx #+# #+# */ +/* Updated: 2022/09/29 03:40:19 by kdx ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *root; + t_list *last; + + if (f == NULL || del == NULL) + return (NULL); + root = ft_lstnew(f(lst->content)); + if (root == NULL) + return (NULL); + last = root; + lst = lst->next; + while (lst != NULL) + { + last->next = ft_lstnew(f(lst->content)); + if (last->next == NULL) + { + ft_lstclear(&root, del); + return (NULL); + } + lst = lst->next; + last = last->next; + } + return (root); +} |