/ fdf / fdf / libft / ft_lstmap.c
ft_lstmap.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   ft_lstmap.c                                        :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <marvin@42.fr>                      +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2022/07/12 15:47:36 by gychoi            #+#    #+#             */
 9  /*   Updated: 2022/07/16 14:24:48 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "libft.h"
14  
15  t_list	*ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
16  {
17  	t_list	*nnew;
18  	t_list	*nlst;
19  
20  	if (!lst || !f)
21  		return (NULL);
22  	nlst = NULL;
23  	while (lst != NULL)
24  	{
25  		nnew = ft_lstnew(f(lst->content));
26  		if (!nnew)
27  		{
28  			ft_lstclear(&nlst, del);
29  			nlst = NULL;
30  			return (NULL);
31  		}
32  		ft_lstadd_back(&nlst, nnew);
33  		lst = lst->next;
34  	}
35  	return (nlst);
36  }