개요
- 연결리스트 lst의 content를 f로 보내고, 그 결과값을 새 연결리스트에 담는 함수.
- 새 연결리스트 할당이 실패하면, 그 연결리스트를 모두 할당 해제한다.
구현
c
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (del)(void *))
{
t_list *new;
t_list *first;
void *content;
first = 0;
if (!lst || !f || !del)
return (0);
while (lst)
{
content = (f)(lst->content);
new = ft_lstnew(content);
if (!new || !(new->content))
{
if (content)
del(content);
free(new);
ft_lstclear(&first, del);
return (0);
}
ft_lstadd_back(&first, new);
lst = lst->next;
}
new = 0;
return (first);
}