-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
45 lines (41 loc) · 1.52 KB
/
ft_lstmap.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyalexan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 10:27:10 by kyalexan #+# #+# */
/* Updated: 2021/12/12 12:30:51 by kyalexan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *ft_lstclearspecial(t_list **new_lst, void (*del)(void *))
{
ft_lstclear(new_lst, del);
return (NULL);
}
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 (NULL);
new_elem = ft_lstnew(f(lst->content));
if (!new_elem)
return (ft_lstclearspecial(&lst, del));
new_lst = new_elem;
lst = lst->next;
while (lst)
{
new_elem = ft_lstnew(f(lst->content));
if (!new_elem)
{
ft_lstclear(&lst, del);
return (ft_lstclearspecial(&new_lst, del));
}
lst = lst->next;
ft_lstadd_back(&new_lst, new_elem);
}
return (new_lst);
}