aboutsummaryrefslogtreecommitdiff
path: root/ft_lstmap_bonus.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-21 20:25:32 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-21 20:25:32 +0200
commit0cf7761bf2985f683b1b73dfe5bdb731672e2b7f (patch)
tree3aff24216409fa5ac313b2ff686f3e23ba9493ff /ft_lstmap_bonus.c
parent70a7a3a54d4e300c7ec75d18d14d6d6b174c9ee4 (diff)
downloadlibft-0cf7761bf2985f683b1b73dfe5bdb731672e2b7f.tar.gz
libft-0cf7761bf2985f683b1b73dfe5bdb731672e2b7f.tar.bz2
libft-0cf7761bf2985f683b1b73dfe5bdb731672e2b7f.zip
Added protection on part2 and changed lstmap according to the subject
Diffstat (limited to 'ft_lstmap_bonus.c')
-rw-r--r--ft_lstmap_bonus.c31
1 files changed, 25 insertions, 6 deletions
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 <stdlib.h>
#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);
+*/