aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_lstmap.c
blob: c001a2d02590db38cf6c8e5ac50b880bdcfc162e (plain)
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
#include "libft.h"
#include <stdlib.h>

t_list *
ft_lstmap (t_list *lst, void *(*f) (void *), void (*del) (void *))
{
  t_list *new_list;
  t_list *node;
  void *content;

  new_list = NULL;
  while (lst)
    {
      content = f (lst->content);
      node = ft_lstnew (content);
      if (!node)
        {
          del (content);
          ft_lstclear (&new_list, del);
          return (NULL);
        }
      ft_lstadd_back (&new_list, node);
      lst = lst->next;
    }
  return (new_list);
}