aboutsummaryrefslogtreecommitdiff
path: root/ft_lstmap_bonus.c
blob: 9ca7cad794b77a861c3d2c7c967bcd2ab72cc934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_lstmap_bonus.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/09 09:03:57 by cacharle          #+#    #+#             */
/*   Updated: 2019/10/21 11:43:10 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include "libft.h"

t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
	t_list	*mapped;
	t_list	*tmp;

	if (lst == NULL || f == NULL || del == NULL)
		return (NULL);
	mapped = NULL;
	while (lst)
	{
		if ((tmp = ft_lstnew((*f)(lst->content))) == NULL)
		{
			ft_lstclear(&tmp, del);
			return (NULL);
		}
		tmp->next = lst->next;
		ft_lstadd_back(&mapped, tmp);
		lst = lst->next;
	}
	return (mapped);
}

/*
** Rest in peace, my beautiful recursion.
**
** if ((tmp = ft_lstnew(lst->content)) == NULL)
** 	return (NULL);
** tmp->content = (*f)(tmp->content);
** tmp->next = ft_lstmap(lst->next, f);
** return (tmp);
*/