From 0cf7761bf2985f683b1b73dfe5bdb731672e2b7f Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 21 Oct 2019 20:25:32 +0200 Subject: Added protection on part2 and changed lstmap according to the subject --- ft_lstmap_bonus.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'ft_lstmap_bonus.c') diff --git a/ft_lstmap_bonus.c b/ft_lstmap_bonus.c index 1dfce78..9ca7cad 100644 --- a/ft_lstmap_bonus.c +++ b/ft_lstmap_bonus.c @@ -13,15 +13,34 @@ #include #include "libft.h" -t_list *ft_lstmap(t_list *lst, void *(*f)(void *)) +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) { t_list *mapped; + t_list *tmp; - if (lst == NULL || f == NULL) + if (lst == NULL || f == NULL || del == NULL) return (NULL); - if ((mapped = ft_lstnew(lst->content)) == NULL) - return (NULL); - mapped->content = (*f)(mapped->content); - mapped->next = ft_lstmap(lst->next, f); + mapped = NULL; + while (lst) + { + if ((tmp = ft_lstnew((*f)(lst->content))) == NULL) + { + ft_lstclear(&tmp, del); + return (NULL); + } + tmp->next = lst->next; + ft_lstadd_back(&mapped, tmp); + lst = lst->next; + } return (mapped); } + +/* +** Rest in peace, my beautiful recursion. +** +** if ((tmp = ft_lstnew(lst->content)) == NULL) +** return (NULL); +** tmp->content = (*f)(tmp->content); +** tmp->next = ft_lstmap(lst->next, f); +** return (tmp); +*/ -- cgit