-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap_bonus.c
52 lines (47 loc) · 1.6 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
52
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 18:21:13 by abdsalah #+# #+# */
/* Updated: 2024/10/21 02:03:53 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_list *add_mapped_node(t_list **head, void *(*f)(void *),
void (*del)(void *), void *content)
{
void *new_content;
t_list *new_node;
new_content = f(content);
if (new_content == NULL)
{
ft_lstclear(head, del);
return (NULL);
}
new_node = ft_lstnew(new_content);
if (new_node == NULL)
{
del(new_content);
ft_lstclear(head, del);
return (NULL);
}
ft_lstadd_back(head, new_node);
return (new_node);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *head;
if (lst == NULL || f == NULL)
return (NULL);
head = NULL;
while (lst != NULL)
{
if (add_mapped_node(&head, f, del, lst->content) == NULL)
return (NULL);
lst = lst->next;
}
return (head);
}