libft/ft_lstmap.c

43 lines
1.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman <timo42@proton.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/24 17:10:04 by tosuman #+# #+# */
/* Updated: 2023/06/30 00:57:49 by tischmid ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
t_list *new_elem;
if (!lst || !f)
return (0);
new_lst = (void *)0;
while (lst)
{
new_elem = ft_lstnew(f(lst->content)); // maybe check for NULL
if (!new_elem)
{
while (new_lst)
{
new_elem = new_lst->next;
del(new_lst->content);
free(new_lst);
new_lst = new_elem;
}
return (0);
}
ft_lstadd_back(&new_lst, new_elem);
lst = lst->next;
}
return (new_lst);
}