Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_lstmap.c File Reference
+ Include dependency graph for ft_lstmap.c:

Go to the source code of this file.

Functions

t_listft_lstmap (t_list *lst, void *(*f)(void *), void(*del)(void *))
 Creates a new linked list with the results of applying a function to each element of the original list. This function takes a linked list and two function pointers, then applies the first function to the content of each element in the list and creates a new list with the results.
 

Function Documentation

◆ ft_lstmap()

t_list * ft_lstmap ( t_list lst,
void *(*)(void *)  f,
void(*)(void *)  del 
)

Creates a new linked list with the results of applying a function to each element of the original list. This function takes a linked list and two function pointers, then applies the first function to the content of each element in the list and creates a new list with the results.

Parameters
lstThe linked list to iterate over.
fThe function to apply to each element.
delThe function to delete each element.
Returns
A pointer to the new linked list, or NULL if an error occurs.

Definition at line 26 of file ft_lstmap.c.

27{
28 t_list *new_list;
29 t_list *new_elem;
30 void *content;
31
32 if (!lst || !f || !del)
33 return (NULL);
34 new_list = NULL;
35 while (lst)
36 {
37 content = f(lst->content);
38 new_elem = ft_lstnew(content);
39 if (!new_elem)
40 {
41 del(content);
42 ft_lstclear(&new_list, del);
43 return (NULL);
44 }
45 ft_lstadd_back(&new_list, new_elem);
46 lst = lst->next;
47 }
48 return (new_list);
49}
void ft_lstadd_back(t_list **lst, t_list *new)
Adds a new element to the end of a linked list. This function takes a pointer to the first element of...
void ft_lstclear(t_list **lst, void(*del)(void *))
Clears a linked list. This function takes a pointer to the first element of a linked list and a funct...
Definition ft_lstclear.c:25
t_list * ft_lstnew(void *content)
Creates a new linked-list node.
Definition ft_lstnew.c:24
Definition types.h:4
struct s_list * next
Definition types.h:6
void * content
Definition types.h:5

References s_list::content, ft_lstadd_back(), ft_lstclear(), ft_lstnew(), and s_list::next.

+ Here is the call graph for this function: