-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
55 lines (50 loc) · 1.53 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
46
47
48
49
50
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: javellis <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 12:46:47 by javellis #+# #+# */
/* Updated: 2022/10/11 12:46:52 by javellis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include<stddef.h>
static t_list *ft_first(t_list *lst, void (*del)(void *), void *(*f)(void *))
{
t_list *res;
res = ft_lstnew(f(lst->content));
if (!res)
{
ft_lstclear(&res, del);
return (NULL);
}
return (res);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *res_begin;
t_list *res;
res_begin = ft_first(lst, del, f);
res = res_begin;
if (!res)
return (NULL);
else
lst = lst->next;
while (lst)
{
res->next = ft_lstnew(f(lst->content));
if (res)
{
lst = lst->next;
res = res->next;
}
else
{
ft_lstclear(&res_begin, del);
return (NULL);
}
}
return (res_begin);
}