This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_lstmap.c
53 lines (48 loc) · 1.77 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <mcombeau@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 20:34:19 by mcombeau #+# #+# */
/* Updated: 2021/12/04 13:13:40 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
DESCRIPTION :
The function ft_lstmap creates a new list from a given list by
applying the function passed as parameter to the original list. If
the memory allocation fails for any node in the new list, the new list
will be deleted with the function passed as parameter and its memory
will be freed.
RETURN VALUE :
The new list containing the new values if a functon was provided.
A new copy of the list if no function was provided.
NULL if the memory allocation failed.
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newlst;
t_list *node;
if (!lst)
return (NULL);
newlst = NULL;
node = NULL;
while (lst)
{
if (!f)
node = ft_lstnew(lst->content);
else
node = ft_lstnew(f(lst->content));
if (!node)
{
ft_lstclear(&newlst, del);
return (NULL);
}
ft_lstadd_back(&newlst, node);
lst = lst->next;
}
return (newlst);
}