tgrekov-libft
HIVE libft Oct 2023
Loading...
Searching...
No Matches
ft_lstmap_bonus.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_lstmap_bonus.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2023/10/28 01:50:13 by tgrekov #+# #+# */
9/* Updated: 2023/11/20 19:22:20 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include "libft.h"
21
35t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
36{
37 t_list *lst2;
38 t_list *new;
39 void *new_content;
40
41 if (!lst)
42 return (0);
43 lst2 = 0;
44 while (lst)
45 {
46 new_content = f(lst->content);
47 new = ft_lstnew(new_content);
48 if (!new)
49 {
50 if (del)
51 del(new_content);
52 ft_lstclear(&lst2, del);
53 return (0);
54 }
55 ft_lstadd_back(&lst2, new);
56 lst = lst->next;
57 }
58 return (lst2);
59}
void ft_lstadd_back(t_list **lst, t_list *new)
Adds node at pointer new to the end of the list starting at pointer pointed to by lst.
void ft_lstclear(t_list **lst, void(*del)(void *))
Delete every node in a list along with it's content.
t_list * ft_lstmap(t_list *lst, void *(*f)(void *), void(*del)(void *))
Iterate over a list and create a new one from the results of applying f to the content of each node.
t_list * ft_lstnew(void *content)
Allocates a new node, set it's content, and return it's address.
Linked list node.
Definition libft.h:44
struct s_list * next
Pointer to the next node.
Definition libft.h:46
void * content
void* to the node's contents.
Definition libft.h:45