summaryrefslogtreecommitdiff
path: root/libft/ft_lstmap.c
blob: e962de61289c125cab437fe6354debc483b11455 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_lstmap.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: kdx    <kdx   @student.42angouleme.fr      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/09/29 03:32:46 by kdx               #+#    #+#             */
/*   Updated: 2022/09/29 03:40:19 by kdx              ###   ########.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);
}