This repository was 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_dlstmap.c
49 lines (46 loc) · 1.79 KB
/
ft_dlstmap.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_dlstmap.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2022/01/25 21:48:31 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* Create a copy of a double linked list and apply a method to every element
* @param[in] *lst The double linked 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_dlist *ft_dlstmap(t_dlist *list, void *(*f)(void *), void (*del)(void *))
{
t_dlist *new_list;
t_ditem *old_elem;
t_ditem *new_elem;
size_t i;
new_list = ft_dlstnew();
if (!new_list)
return (NULL);
i = 0;
old_elem = list->first;
while (i < list->size)
{
new_elem = ft_ditemnew((*f)(old_elem->content));
if (!new_elem)
{
ft_dlstclear(new_list, del);
return (NULL);
}
ft_dlstadd_back(new_list, new_elem);
old_elem = old_elem->next;
i++;
}
return (new_list);
}