42_libft/ft_lstmap.c
Camille Chauvet 80df47ddb4 d
2022-10-05 21:04:45 +02:00

40 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
/* Updated: 2022/10/05 00:08:45 by cchauvet ### ########.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);
}