diff --git a/Makefile b/Makefile index 6c7bd58..40887a5 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gbrochar +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2015/11/27 19:26:17 by gbrochar #+# #+# # -# Updated: 2024/10/13 18:44:22 by gbrochar ### ########.fr # +# Updated: 2024/10/14 20:59:43 by gbrochar ### ########.fr # # # # **************************************************************************** # @@ -22,7 +22,7 @@ ft_strchr.c ft_strclr.c ft_strcmp.c ft_strcpy.c ft_strdel.c ft_strdup.c \ ft_strequ.c ft_striter.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlen.c \ ft_strmap.c ft_strmapi.c ft_strncat.c ft_strncmp.c ft_strncpy.c ft_strnequ.c \ ft_strnew.c ft_strnstr.c ft_strrchr.c ft_strsplit.c ft_strstr.c ft_strsub.c \ -ft_strtrim.c ft_swap.c ft_tolower.c ft_toupper.c ft_lstadd_back.c +ft_strtrim.c ft_swap.c ft_tolower.c ft_toupper.c ft_lstadd_back.c ft_lstinsert.c OBJ = $(SRC:.c=.o) diff --git a/ft_lstadd_back.c b/ft_lstadd_back.c index 750452d..91c6e10 100644 --- a/ft_lstadd_back.c +++ b/ft_lstadd_back.c @@ -2,6 +2,10 @@ void ft_lstadd_back(t_list **alst, t_list *new) { + if (!alst) { + (*alst) = new; + return; + } while ((*alst)->next) alst = &(*alst)->next; (*alst)->next = new; diff --git a/ft_lstinsert.c b/ft_lstinsert.c new file mode 100644 index 0000000..e1fd61e --- /dev/null +++ b/ft_lstinsert.c @@ -0,0 +1,14 @@ +#include "libft.h" + +void ft_lstinsert(t_list **lst, t_list *new, int (*f)(t_list *a, t_list *b)) { + if (!(*lst)) { + *lst = new; + return; + } + while ((*lst)->next && f(new, *lst) < 0) { + lst = &(*lst)->next; + } + t_list *tmp = (*lst)->next; + (*lst)->next = new; + new->next = tmp; +} diff --git a/libft.h b/libft.h index 53a8fbd..35acffe 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2015/11/24 12:52:28 by gbrochar #+# #+# */ -/* Updated: 2024/10/13 18:45:37 by gbrochar ### ########.fr */ +/* Updated: 2024/10/14 21:01:47 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ typedef struct s_list void ft_lstadd(t_list **alst, t_list *new); void ft_lstadd_back(t_list **alst, t_list *new); +void ft_lstinsert(t_list **lst, t_list *new, int (*f)(t_list *a, t_list *b)); void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); t_list *ft_lstnew(void const *content, size_t content_size);