summaryrefslogtreecommitdiff
path: root/ft_lstmap_bonus.c
diff options
context:
space:
mode:
Diffstat (limited to 'ft_lstmap_bonus.c')
-rw-r--r--ft_lstmap_bonus.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/ft_lstmap_bonus.c b/ft_lstmap_bonus.c
new file mode 100644
index 0000000..91eabfc
--- /dev/null
+++ b/ft_lstmap_bonus.c
@@ -0,0 +1,43 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstmap_bonus.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/03/10 16:45:21 by dkaiser #+# #+# */
+/* Updated: 2024/03/10 19:03:00 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
+{
+ t_list *result;
+ t_list *new;
+
+ if (!lst || !f || !del)
+ return (NULL);
+ new = ft_lstnew(f(lst->content));
+ if (!new)
+ {
+ free(new);
+ return (NULL);
+ }
+ result = new;
+ lst = lst->next;
+ while (lst)
+ {
+ new->next = ft_lstnew(f(lst->content));
+ if (!new->next)
+ {
+ ft_lstclear(&new, del);
+ return (NULL);
+ }
+ new = new->next;
+ lst = lst->next;
+ }
+ // new->next = NULL;
+ return (result);
+}