-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_lstmap.c
More file actions
46 lines (43 loc) · 1.68 KB
/
ft_lstmap.c
File metadata and controls
46 lines (43 loc) · 1.68 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdragan <rdragan@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/06 11:16:28 by rdragan #+# #+# */
/* Updated: 2023/04/26 15:38:54 by rdragan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Iterates the list lst and applies the fuction f on the content of each node.
Creates a list resulting onn the sccessive applicateion of f. The del
is used to delete the content of a node if needed.
@param lst: address of the first list node.
@param f: address of the function used to iterate on the list.
@param del: address of the function used to delete the content of a node.
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *))
{
t_list *new_lst;
t_list *new_node;
if ((!lst) || !f)
return (NULL);
new_lst = ft_lstnew(f(lst->content));
if (!new_lst)
return (NULL);
lst = lst->next;
while (lst)
{
new_node = ft_lstnew(f(lst->content));
if (!new_node)
{
ft_lstclear(&new_lst);
return (NULL);
}
ft_lstadd_back(&new_lst, new_node);
lst = lst->next;
}
return (new_lst);
}