-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
50 lines (45 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edboutil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/17 19:19:33 by edboutil #+# #+# */
/* Updated: 2022/11/17 19:23:45 by edboutil ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_list *ft_lstnew_free(void *content)
{
t_list *lst;
lst = (t_list *)malloc(sizeof(*lst));
if (!lst)
{
free(content);
return (NULL);
}
lst->content = content;
lst->next = NULL;
return (lst);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
t_list *start;
if (!lst && !f)
return (0);
start = NULL;
while (lst)
{
new_lst = ft_lstnew_free(f(lst->content));
if (!new_lst)
{
ft_lstclear(&start, del);
return (NULL);
}
ft_lstadd_back(&start, new_lst);
lst = lst->next;
}
return (start);
}