This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
executable file
·45 lines (42 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_lstmap.c :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/03 17:42:36 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* Create a copy of a linked list and apply a method to every element
* @param[in] *lst The list to copy
* @param[in] void *(*f)(void *) The method to apply to every element of the
* copied list
* @param[in] void (*del)(void *) The method applied to every element before
* deletion (of the copy) if anything goes wrong
* @return The copied list after the applied method
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_list;
t_list *new_elem;
new_list = ft_lstnew((*f)(lst->content));
if (!new_list)
return (NULL);
lst = lst->next;
while (lst)
{
new_elem = ft_lstnew((*f)(lst->content));
if (!new_elem)
{
ft_lstclear(&new_list, del);
return (NULL);
}
ft_lstadd_back(&new_list, new_elem);
lst = lst->next;
}
return (new_list);
}