-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap_bonus.c
51 lines (47 loc) · 1.81 KB
/
ft_lstmap_bonus.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_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 13:38:10 by lclerc #+# #+# */
/* Updated: 2022/11/24 15:31:46 by lclerc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* ft_lstmap() iterates the list 'lst' and applies the function 'f' on the con-
* -tent of each node. It creates a new list resulting of the successive
* applications of the function 'f'. The 'del' function is used to delete the
* content of a node if needed.
*
* 'lst' is the address of a pointer to a node.
* 'f' is the address of a function used to iterate on the list.
* 'del' is the address of the function used to delete the content of a node
* if needed.
*
* Return value:
* The new list.
* NULL if the allocation fails.
*
* Allowed functions malloc() and free().
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void*))
{
t_list *new_lst;
t_list *new_node;
new_lst = NULL;
while (lst)
{
new_node = ft_lstnew((*f)(lst->content));
if (!new_node)
{
ft_lstclear(&new_lst, del);
return (NULL);
}
ft_lstadd_back(&new_lst, new_node);
lst = lst->next;
}
return (new_lst);
}