Skip to content

Commit 6ad1235

Browse files
committed
feat: Add ft_lstdup() to libft
1 parent 74bd969 commit 6ad1235

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

libraries/libft/build/libft.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ SRC += $(addprefix $(DIR), \
9090
ft_lstclear.c \
9191
ft_lstdelone.c \
9292
ft_lstdrop_node.c \
93+
ft_lstdup.c \
9394
ft_lstinsert_after.c \
9495
ft_lstinsert_before.c \
9596
ft_lstiter.c \

libraries/libft/inc/libft.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */
9-
/* Updated: 2024/06/07 18:28:17 by ldulling ### ########.fr */
9+
/* Updated: 2024/06/07 18:59:14 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -70,6 +70,7 @@ void ft_lstadd_front(t_list **lst, t_list *new);
7070
void ft_lstclear(t_list **lst, void (*del)(void *));
7171
void ft_lstdelone(t_list *lst, void (*del)(void *));
7272
void ft_lstdrop_node(t_list **lst, t_list **node, void (*del)(void *));
73+
t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *));
7374
void ft_lstinsert_after(t_list **lst, t_list *new);
7475
void ft_lstinsert_before(t_list **lst, t_list *cur, t_list *new);
7576
void ft_lstiter(t_list *lst, void (*f)(void *));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_lstdup.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/02/11 00:35:12 by ldulling #+# #+# */
9+
/* Updated: 2024/06/07 19:09:20 by ldulling ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
/**
16+
* The ft_lstdup function duplicates a singly linked list by creating new nodes
17+
* with the same content as the original list. The content of the new nodes is
18+
* created by calling the provided 'dup' function on the content of the original
19+
* nodes.
20+
*
21+
* @param lst The original list to be duplicated.
22+
* @param dup The function to duplicate the content of the nodes.
23+
* @param del The function to delete the content of the nodes in case of
24+
* failure.
25+
*
26+
* @return Returns a pointer to the first node of the new list, or NULL if
27+
* the list could not be duplicated.
28+
*
29+
*/
30+
t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *))
31+
{
32+
t_list *cur;
33+
t_list *new_lst;
34+
t_list *new_lst_tail;
35+
void *new_content;
36+
37+
if (lst == NULL || dup == NULL || del == NULL)
38+
return (NULL);
39+
new_lst = NULL;
40+
new_lst_tail = NULL;
41+
cur = lst;
42+
while (cur != NULL)
43+
{
44+
new_content = (*dup)(cur->content);
45+
if (new_content == NULL)
46+
return (ft_lstclear(&new_lst, del), NULL);
47+
if (!ft_lstnew_back_eff(&new_lst, &new_lst_tail, new_content))
48+
return (del(new_content), ft_lstclear(&new_lst, del), NULL);
49+
cur = cur->next;
50+
}
51+
return (new_lst);
52+
}

0 commit comments

Comments
 (0)