From 176dfd9fd936e853cf87239219416ee774602dcf Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Wed, 16 Aug 2023 17:46:11 +0200 Subject: [PATCH] Added bonus functions and changed libft.h and Makefile accordingly. --- Makefile | 10 ++++++++- ft_lstadd_back.c | 25 ++++++++++++++++++++++ ft_lstadd_front.c | 20 ++++++++++++++++++ ft_lstclear.c | 27 ++++++++++++++++++++++++ ft_lstdelone.c | 21 ++++++++++++++++++ ft_lstiter.c | 23 ++++++++++++++++++++ ft_lstlast.c | 23 ++++++++++++++++++++ ft_lstmap.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ ft_lstnew.c | 26 +++++++++++++++++++++++ ft_lstsize.c | 29 +++++++++++++++++++++++++ libft.h | 22 ++++++++++++++++++- 11 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 ft_lstadd_back.c create mode 100644 ft_lstadd_front.c create mode 100644 ft_lstclear.c create mode 100644 ft_lstdelone.c create mode 100644 ft_lstiter.c create mode 100644 ft_lstlast.c create mode 100644 ft_lstmap.c create mode 100644 ft_lstnew.c create mode 100644 ft_lstsize.c diff --git a/Makefile b/Makefile index 1ae2e12..3f51445 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,11 @@ INCDIR = . SRCDIR = . # SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c) SOURCES = $(shell find $(SRCDIR) -name "*.c") +BONUS = $(shell find $(SRCDIR) -name "*lst*.c") +TEMP := $(SOURCES) +SOURCES = $(filter-out $(BONUS), $(TEMP)) OBJECTS = $(SOURCES:.c=.o) +BONUS_OBJECTS = $(BONUS:.c=.o) NAME = libft.a @@ -21,10 +25,14 @@ $(NAME) : $(OBJECTS) $(CC) $(CFLAGS) -o $@ -c $< -I $(INCDIR) clean : - $(RM) $(OBJECTS) + $(RM) $(OBJECTS) $(BONUS_OBJECTS) fclean : clean $(RM) $(NAME) re : fclean $(MAKE) all + +#bonus : CFLAGS += -DBONUS +bonus : $(OBJECTS) $(BONUS_OBJECTS) + $(AR) rcs $(NAME) $(OBJECTS) $(BONUS_OBJECTS) diff --git a/ft_lstadd_back.c b/ft_lstadd_back.c new file mode 100644 index 0000000..be63a9e --- /dev/null +++ b/ft_lstadd_back.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:33:28 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 16:52:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + if (*lst == NULL) + { + *lst = new; + return ; + } + ft_lstlast(*lst)->next = new; + return ; +} diff --git a/ft_lstadd_front.c b/ft_lstadd_front.c new file mode 100644 index 0000000..4995275 --- /dev/null +++ b/ft_lstadd_front.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:26:20 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:42:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; + return ; +} diff --git a/ft_lstclear.c b/ft_lstclear.c new file mode 100644 index 0000000..afd9211 --- /dev/null +++ b/ft_lstclear.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 16:48:58 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *temp; + + while (*lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = temp; + } + return ; +} diff --git a/ft_lstdelone.c b/ft_lstdelone.c new file mode 100644 index 0000000..622b3eb --- /dev/null +++ b/ft_lstdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:46:37 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + del(lst->content); + free(lst); + return ; +} diff --git a/ft_lstiter.c b/ft_lstiter.c new file mode 100644 index 0000000..c188fc0 --- /dev/null +++ b/ft_lstiter.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:50:31 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:51:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->content); + lst = lst->next; + } + return ; +} diff --git a/ft_lstlast.c b/ft_lstlast.c new file mode 100644 index 0000000..c544009 --- /dev/null +++ b/ft_lstlast.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:31:01 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 17:14:03 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (lst); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/ft_lstmap.c b/ft_lstmap.c new file mode 100644 index 0000000..d6810cc --- /dev/null +++ b/ft_lstmap.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:51:57 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 16:58:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static t_list *first_el(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + void *cont; + + if (lst == NULL) + return (NULL); + cont = f(lst->content); + res = ft_lstnew(cont); + if (res == NULL) + del(cont); + return (res); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + t_list *new_node; + void *new_cont; + + res = first_el(lst, f, del); + if (res == NULL) + return (res); + new_node = res; + lst = lst->next; + while (lst && res) + { + new_cont = f(lst->content); + new_node->next = ft_lstnew(new_cont); + new_node = new_node->next; + if (new_node == NULL) + { + del(new_cont); + ft_lstclear(&res, del); + } + lst = lst->next; + } + return (res); +} diff --git a/ft_lstnew.c b/ft_lstnew.c new file mode 100644 index 0000000..912fdc7 --- /dev/null +++ b/ft_lstnew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:23:00 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:26:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *res; + + res = malloc(sizeof(t_list)); + if (res == NULL) + return (res); + res->content = content; + res->next = NULL; + return (res); +} diff --git a/ft_lstsize.c b/ft_lstsize.c new file mode 100644 index 0000000..cb2dc8f --- /dev/null +++ b/ft_lstsize.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:28:58 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 14:59:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int res; + + if (lst == NULL) + return (0); + res = 1; + while (lst->next) + { + lst = lst->next; + ++res; + } + return (res); +} diff --git a/libft.h b/libft.h index 28f01be..1d8eb25 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:42:58 by ljiriste ### ########.fr */ +/* Updated: 2023/08/16 17:12:57 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,4 +61,24 @@ void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); +//# ifdef BONUS + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +//# endif + #endif -- 2.30.2