-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
109 lines (88 loc) · 2.59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: musenov <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 15:28:53 by musenov #+# #+# */
/* Updated: 2022/12/06 00:31:18 by musenov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
t_list *space_holder;
void *f_ptr;
if (!f || !del)
return (NULL);
new_lst = NULL;
while (lst != NULL)
{
f_ptr = f((*lst).content);
space_holder = ft_lstnew(f_ptr);
if (space_holder == NULL)
{
del(f_ptr);
ft_lstclear(&new_lst, del);
return (NULL);
}
ft_lstadd_back(&new_lst, space_holder);
lst = (*lst).next;
}
return (new_lst);
}
/*
ft_lstmap:
PARAMETERS
lst: The address of a pointer to a node.
f: The address of the function used to iterate on the list.
del: 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.
DESCRIPTION
Iterates the list ’lst’ and applies the function ’f’ on the
content of each node. 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.
QUESTIONS
-/-
ANSWER
-/-
COMPARE
-/-
ALTERNATIVE SOLUTION
-/-
EXPLANATION
of why ft_lstmap failed "francintee --strict" test
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
t_list *space_holder;
void *f_ptr;
if (!f || !del)
return (NULL);
new_lst = NULL;
while (lst != NULL)
{
// f function can also use malloc, and if it fails to allocate the
// memory, then f_prt must be also deleted, see line "del(f_ptr)" below
f_ptr = f((*lst).content);
space_holder = ft_lstnew(f_ptr);
if (space_holder == NULL)
{
del(f_ptr);
ft_lstclear(&new_lst, del);
return (NULL);
}
ft_lstadd_back(&new_lst, space_holder);
lst = (*lst).next;
}
return (new_lst);
}
REMARK
-/-
*/