/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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);
}