diff --git a/libraries/libft/build/libft.mk b/libraries/libft/build/libft.mk index fef28ba0..b8b8c082 100644 --- a/libraries/libft/build/libft.mk +++ b/libraries/libft/build/libft.mk @@ -6,7 +6,7 @@ # By: ldulling +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/16 13:33:38 by ldulling #+# #+# # -# Updated: 2024/06/28 19:31:23 by ldulling ### ########.fr # +# Updated: 2025/05/28 19:26:09 by ldulling ### ########.fr # # # # **************************************************************************** # @@ -79,16 +79,19 @@ SRC += $(addprefix $(DIR), \ ft_lstlast_d.c \ ft_lstnew_back_d.c \ ft_lstnew_d.c \ + ft_lstsize_d.c \ ) # Singly-linked: DIR := lists/singly_linked/ SRC += $(addprefix $(DIR), \ ft_lstadd_back.c \ + ft_lstadd_back_tail.c \ ft_lstadd_front.c \ ft_lstclear.c \ ft_lstdelone.c \ ft_lstdrop_node.c \ + ft_lstdup.c \ ft_lstinsert_after.c \ ft_lstinsert_before.c \ ft_lstiter.c \ @@ -96,6 +99,7 @@ SRC += $(addprefix $(DIR), \ ft_lstmap.c \ ft_lstnew.c \ ft_lstnew_back.c \ + ft_lstnew_back_tail.c \ ft_lstnew_front.c \ ft_lstpop_front.c \ ft_lstpop_front_content.c \ diff --git a/libraries/libft/inc/libft.h b/libraries/libft/inc/libft.h index 5ebb7aa9..efa17727 100644 --- a/libraries/libft/inc/libft.h +++ b/libraries/libft/inc/libft.h @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */ -/* Updated: 2024/05/21 14:58:24 by ldulling ### ########.fr */ +/* Updated: 2025/05/28 19:26:17 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,15 +60,18 @@ void ft_lstiter_d(t_list_d *lst, void (*f)(void *)); t_list_d *ft_lstlast_d(t_list_d *lst); bool ft_lstnew_back_d(t_list_d **lst, void *content); t_list_d *ft_lstnew_d(void *content); +int ft_lstsize_d(t_list_d *lst); \ /* Lists singly-linked */ void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_back_tail(t_list **lst, t_list **tail, t_list *new); void ft_lstadd_front(t_list **lst, t_list *new); void ft_lstclear(t_list **lst, void (*del)(void *)); void ft_lstdelone(t_list *lst, void (*del)(void *)); void ft_lstdrop_node(t_list **lst, t_list **node, void (*del)(void *)); +t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *)); void ft_lstinsert_after(t_list **lst, t_list *new); void ft_lstinsert_before(t_list **lst, t_list *cur, t_list *new); void ft_lstiter(t_list *lst, void (*f)(void *)); @@ -76,6 +79,7 @@ t_list *ft_lstlast(t_list *lst); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); t_list *ft_lstnew(void *content); bool ft_lstnew_back(t_list **lst, void *content); +bool ft_lstnew_back_tail(t_list **lst, t_list **tail, void *content); bool ft_lstnew_front(t_list **lst, void *content); t_list *ft_lstpop_front(t_list **lst); void *ft_lstpop_front_content(t_list **lst); diff --git a/libraries/libft/src/lists/doubly_linked/ft_lstsize_d.c b/libraries/libft/src/lists/doubly_linked/ft_lstsize_d.c new file mode 100644 index 00000000..0f1b81a4 --- /dev/null +++ b/libraries/libft/src/lists/doubly_linked/ft_lstsize_d.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize_d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ldulling +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/24 16:08:06 by ldulling #+# #+# */ +/* Updated: 2024/06/28 19:30:32 by ldulling ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize_d(t_list_d *lst) +{ + int n; + t_list_d *cur; + + n = 0; + cur = lst; + while (cur != NULL) + { + n++; + cur = cur->next; + } + return (n); +} diff --git a/libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c b/libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c new file mode 100644 index 00000000..de1d6cf8 --- /dev/null +++ b/libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back_tail.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ldulling +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 00:35:15 by ldulling #+# #+# */ +/* Updated: 2025/05/28 21:34:35 by ldulling ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * The ft_lstadd_back_tail function adds a node to the end of the singly linked + * list. + * It uses the tail pointer of the list to avoid traversing the whole list. + * The tail pointer is updated to point to the new node. + * + * @param lst The address of the pointer to the first node of the list. + * @param tail The address of the pointer to the last node of the list. + * If tail (not *tail) is NULL, it is ignored. + * @param new The new node to be added to the list. + * If new is NULL, the function does nothing. + * + * @return No return value. + * + */ +void ft_lstadd_back_tail(t_list **lst, t_list **tail, t_list *new) +{ + if (lst == NULL || tail == NULL || new == NULL) + { + ft_lstadd_back(lst, new); + return ; + } + if (*lst == NULL || *tail == NULL) + ft_lstadd_back(lst, new); + else + (*tail)->next = new; + *tail = new; + return ; +} diff --git a/libraries/libft/src/lists/singly_linked/ft_lstdup.c b/libraries/libft/src/lists/singly_linked/ft_lstdup.c new file mode 100644 index 00000000..8f953c80 --- /dev/null +++ b/libraries/libft/src/lists/singly_linked/ft_lstdup.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ldulling +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/11 00:35:12 by ldulling #+# #+# */ +/* Updated: 2025/05/28 19:27:00 by ldulling ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * The ft_lstdup function duplicates a singly linked list by creating new nodes + * with the same content as the original list. The content of the new nodes is + * created by calling the provided 'dup' function on the content of the original + * nodes. + * + * @param lst The original list to be duplicated. + * @param dup The function to duplicate the content of the nodes. + * @param del The function to delete the content of the nodes in case of + * failure. + * + * @return Returns a pointer to the first node of the new list, or NULL if + * the list could not be duplicated. + * + */ +t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *)) +{ + t_list *cur; + t_list *new_lst; + t_list *new_lst_tail; + void *new_content; + + if (lst == NULL || dup == NULL || del == NULL) + return (NULL); + new_lst = NULL; + new_lst_tail = NULL; + cur = lst; + while (cur != NULL) + { + new_content = (*dup)(cur->content); + if (new_content == NULL) + return (ft_lstclear(&new_lst, del), NULL); + if (!ft_lstnew_back_tail(&new_lst, &new_lst_tail, new_content)) + return (del(new_content), ft_lstclear(&new_lst, del), NULL); + cur = cur->next; + } + return (new_lst); +} diff --git a/libraries/libft/src/lists/singly_linked/ft_lstmap.c b/libraries/libft/src/lists/singly_linked/ft_lstmap.c index faf7cc48..ba2d16a0 100644 --- a/libraries/libft/src/lists/singly_linked/ft_lstmap.c +++ b/libraries/libft/src/lists/singly_linked/ft_lstmap.c @@ -6,14 +6,12 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/24 16:04:35 by ldulling #+# #+# */ -/* Updated: 2023/12/23 11:43:14 by ldulling ### ########.fr */ +/* Updated: 2025/05/28 19:27:05 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node); - t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) { t_list *cur; @@ -37,18 +35,8 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) ft_lstclear(&new_lst, del); return (NULL); } - lstadd_back_eff(&new_lst, &new_lst_tail, &new_node); + ft_lstadd_back_tail(&new_lst, &new_lst_tail, new_node); cur = cur->next; } return (new_lst); } - -static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node) -{ - if (*head == NULL) - *head = *new_node; - else - (*tail)->next = *new_node; - *tail = *new_node; - return ; -} diff --git a/libraries/libft/src/lists/singly_linked/ft_lstnew_back.c b/libraries/libft/src/lists/singly_linked/ft_lstnew_back.c index c1200880..3e2986bf 100644 --- a/libraries/libft/src/lists/singly_linked/ft_lstnew_back.c +++ b/libraries/libft/src/lists/singly_linked/ft_lstnew_back.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/06 20:30:19 by ldulling #+# #+# */ -/* Updated: 2024/01/17 11:42:57 by ldulling ### ########.fr */ +/* Updated: 2025/05/28 20:14:28 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,15 @@ * @param content The content to be added to the new node. * * @return Returns true if the new node was successfully added, false - * if malloc failed. + * if malloc failed or lst (not *lst) is NULL. * */ bool ft_lstnew_back(t_list **lst, void *content) { t_list *new_node; + if (lst == NULL) + return (false); new_node = ft_lstnew(content); if (new_node == NULL) return (false); diff --git a/libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c b/libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c new file mode 100644 index 00000000..6138e8e6 --- /dev/null +++ b/libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew_back_tail.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ldulling +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/07 19:11:17 by ldulling #+# #+# */ +/* Updated: 2025/05/28 20:24:43 by ldulling ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * The ft_lstnew_back_tail function creates a new node with the provided content + * and adds it to the end of the singly linked list. + * It uses the tail pointer of the list to avoid traversing the whole list. + * The tail pointer is updated to point to the new node. + * + * @param lst The address of the list to add the new node to. + * @param tail The address of the pointer to the last node of the list. + * If tail (not *tail) is NULL, it is ignored. + * @param content The content to be added to the new node. + * + * @return Returns true if the new node was successfully added, false + * if malloc failed or lst (not *lst) is NULL. + * + */ +bool ft_lstnew_back_tail(t_list **lst, t_list **tail, void *content) +{ + t_list *new_node; + + if (lst == NULL || tail == NULL) + return (ft_lstnew_back(lst, content)); + new_node = ft_lstnew(content); + if (new_node == NULL) + return (false); + ft_lstadd_back_tail(lst, tail, new_node); + return (true); +} diff --git a/libraries/libft/src/lists/singly_linked/ft_lstnew_front.c b/libraries/libft/src/lists/singly_linked/ft_lstnew_front.c index dfa1c51a..80a732d6 100644 --- a/libraries/libft/src/lists/singly_linked/ft_lstnew_front.c +++ b/libraries/libft/src/lists/singly_linked/ft_lstnew_front.c @@ -6,7 +6,7 @@ /* By: ldulling +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/25 16:09:14 by ldulling #+# #+# */ -/* Updated: 2024/01/25 16:09:17 by ldulling ### ########.fr */ +/* Updated: 2025/05/28 20:14:18 by ldulling ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,15 @@ * @param content The content to be added to the new node. * * @return Returns true if the new node was successfully added, false if - * malloc failed. + * malloc failed or lst (not *lst) is NULL. * */ bool ft_lstnew_front(t_list **lst, void *content) { t_list *new_node; + if (lst == NULL) + return (false); new_node = ft_lstnew(content); if (new_node == NULL) return (false);