-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
48 lines (43 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agras <agras@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 19:48:00 by agras #+# #+# */
/* Updated: 2021/12/12 23:48:42 by agras ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_list *elem_dup(t_list *lst, t_list *new_lst, void *(*f)(void *))
{
t_list *new;
new = malloc(sizeof(t_list));
if (!new)
return (NULL);
new->content = f(lst->content);
new->next = NULL;
ft_lstadd_back(&new_lst, new);
return (new_lst);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
if (!lst)
return (NULL);
new_lst = ft_lstnew(f(lst->content));
if (!new_lst)
return (NULL);
lst = lst->next;
while (lst)
{
if (elem_dup(lst, new_lst, f) == NULL)
{
ft_lstclear(&new_lst, del);
return (NULL);
}
lst = lst->next;
}
return (new_lst);
}