-
Notifications
You must be signed in to change notification settings - Fork 4
/
ft_lstmap.c
47 lines (43 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vbrazhni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/07/04 13:44:19 by vbrazhni #+# #+# */
/* Updated: 2018/07/04 13:44:20 by vbrazhni ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static void ft_del(void *content, size_t content_size)
{
(void)content_size;
free(content);
}
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *elem;
t_list *prev;
t_list *head;
prev = NULL;
head = NULL;
if (f)
while (lst)
{
if (!(elem = f(lst)))
{
if (head)
ft_lstdel(&head, &ft_del);
return (NULL);
}
if (prev)
prev->next = elem;
else
head = elem;
lst = lst->next;
prev = elem;
}
return (head);
}