-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
51 lines (47 loc) · 1.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <rbetz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/12 11:15:33 by rbetz #+# #+# */
/* Updated: 2022/04/13 15:18:29 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check(t_list *head)
{
if (head->content == NULL)
{
free(head);
return (0);
}
else
return (1);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newhead;
t_list *tmp;
if (lst == NULL || f == NULL)
return (NULL);
newhead = ft_lstnew(f(lst->content));
tmp = newhead;
if (!ft_check(newhead))
return (NULL);
lst = lst->next;
while (lst != NULL)
{
ft_lstadd_back(&newhead, ft_lstnew(f(lst->content)));
tmp = tmp->next;
lst = lst->next;
if (tmp->content == NULL)
{
ft_lstclear(&newhead, del);
free(newhead);
return (NULL);
}
}
return (newhead);
}