-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap_bonus.c
49 lines (46 loc) · 1.92 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flverge <flverge@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 20:19:07 by flverge #+# #+# */
/* Updated: 2024/09/22 17:22:13 by flverge ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @file ft_lstmap_bonus.c
* @brief Applies a function to each element of a list and creates a new list.
*
* This function iterates over the given list `lst`, applies the function `f`
* to the content of each element, and creates a new list with the results.
* If memory allocation fails during the process, the function uses `del` to
* free the content of the new list and returns NULL.
*
* @param lst The address of a pointer to the first element of the list.
* @param f The function to apply to each element's content.
* @param del The function to delete the content of an element if needed.
* @return The new list, or NULL if memory allocation fails.
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_list;
t_list *new_node;
if (!lst || !f)
return (NULL);
new_list = NULL;
while (lst)
{
new_node = ft_lstnew(f(lst->content));
if (!new_node)
{
ft_lstclear(&new_list, del);
return (NULL);
}
ft_lstadd_back(&new_list, new_node);
lst = lst->next;
}
return (new_list);
}