so_long/libft/ft_lstmap.c
2022-12-11 18:48:37 +01:00

43 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 01:03:28 by erey-bet #+# #+# */
/* Updated: 2022/10/10 14:55:38 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *head;
t_list *new;
if (lst == NULL || f == NULL || del == NULL)
return (NULL);
new = ft_lstnew(f(lst->content));
if (new == NULL)
{
ft_lstclear(&new, del);
return (NULL);
}
head = new;
lst = lst->next;
while (lst != NULL)
{
new->next = ft_lstnew(f(lst->content));
if (new == NULL)
{
ft_lstclear(&head, del);
return (NULL);
}
new = new->next;
lst = lst->next;
}
return (head);
}