-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_lstmap.c
55 lines (50 loc) · 1.65 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: gamarcha <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/15 21:17:26 by gamarcha #+# #+# */
/* Updated: 2021/04/15 21:17:26 by gamarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *ft_freelst(t_list *lst, void (*del)(void *))
{
t_list *tmp;
while (lst)
{
tmp = lst;
lst = lst->next;
if (del)
del(tmp->content);
free(tmp);
}
return (0);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new;
t_list *node;
if (!lst)
return (0);
new = (t_list *)malloc(sizeof(t_list));
if (new == 0)
return (0);
new->content = f(lst->content);
new->next = 0;
node = new;
lst = lst->next;
while (lst)
{
node->next = (t_list *)malloc(sizeof(t_list));
if (node->next == 0)
return (ft_freelst(new, del));
node->next->content = f(lst->content);
node->next->next = 0;
node = node->next;
lst = lst->next;
}
return (new);
}