42_libft/ft_lstmap.c

40 lines
1.3 KiB
C
Raw Permalink Normal View History

2022-09-27 11:25:41 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_lstmap.c :+: :+: :+: */
2022-09-27 11:25:41 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-04 08:21:55 -04:00
/* Created: 2022/09/30 00:05:05 by cchauvet #+# #+# */
2022-10-05 15:04:45 -04:00
/* Updated: 2022/10/05 00:08:45 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-05 15:04:45 -04:00
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);
}