diff --git a/Makefile b/Makefile index fa8085a..2a8896b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2023/11/11 15:10:24 by adjoly ### ########.fr # +# Updated: 2023/11/11 18:38:12 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -49,8 +49,20 @@ SRCS = ft_atoi.c \ ft_strmapi.c \ ft_striteri.c \ +SRCS_BONUS= ft_lstnew.c \ + ft_lstadd_front.c \ + ft_lstsize.c \ + ft_lstlast.c \ + ft_lstadd_back.c \ + ft_lstdelone.c \ + ft_lstclear.c \ + ft_lstiter.c \ + ft_lstmap.c \ + OBJS = $(SRCS:.c=.o) +OBJS_BONUS = $(SRCS_BONUS:.c=.o) + FLAGS = -Werror -Wall -Wextra HEADER = libft.h @@ -59,14 +71,17 @@ $(NAME): $(OBJS) ar -rcs $(NAME) $(OBJS) # so: - # $(CC) -nostartfiles -fPIC $(CFLAGS) $(SRCS) - # gcc -nostartfiles -shared -o libft.so $(OBJS) + # $(CC) -nostartfiles -fPIC $(CFLAGS) $(SRCS) $(SRCS_BONUS) + # gcc -nostartfiles -shared -o libft.so $(OBJS) $(OBJS_BONUS) %.o: %.c $(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ all: $(NAME) +bonus: $(OBJS_BONUS) + ar -rcs $(NAME) $(OBJS_BONUS) + clean: rm -f $(OBJS) diff --git a/ft_lstadd_back.c b/ft_lstadd_back.c new file mode 100644 index 0000000..f1c1b98 --- /dev/null +++ b/ft_lstadd_back.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 16:37:42 by adjoly #+# #+# */ +/* Updated: 2023/11/11 17:56:48 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + if (lst == NULL) + return ; + if (*lst == NULL) + { + (*lst) = new; + return ; + } + ft_lstlast((*lst))->next = new; +} diff --git a/ft_lstadd_back.o b/ft_lstadd_back.o new file mode 100644 index 0000000..8cf09a8 Binary files /dev/null and b/ft_lstadd_back.o differ diff --git a/ft_lstadd_front.c b/ft_lstadd_front.c new file mode 100644 index 0000000..41bec2b --- /dev/null +++ b/ft_lstadd_front.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 15:39:25 by adjoly #+# #+# */ +/* Updated: 2023/11/11 15:50:06 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (lst == NULL) + return ; + if (new == NULL) + return ; + new->next = *lst; + *lst = new; +} diff --git a/ft_lstadd_front.o b/ft_lstadd_front.o new file mode 100644 index 0000000..4e0634b Binary files /dev/null and b/ft_lstadd_front.o differ diff --git a/ft_lstclear.c b/ft_lstclear.c new file mode 100644 index 0000000..69de6c7 --- /dev/null +++ b/ft_lstclear.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 18:05:14 by adjoly #+# #+# */ +/* Updated: 2023/11/11 18:19:34 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + tmp = NULL; + while (*lst && lst) + { + tmp = (*lst)->next; + ft_lstdelone((*lst), del); + *lst = tmp; + } +} diff --git a/ft_lstclear.o b/ft_lstclear.o new file mode 100644 index 0000000..8bf87e0 Binary files /dev/null and b/ft_lstclear.o differ diff --git a/ft_lstdelone.c b/ft_lstdelone.c new file mode 100644 index 0000000..13ce485 --- /dev/null +++ b/ft_lstdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 17:59:39 by adjoly #+# #+# */ +/* Updated: 2023/11/11 18:17:04 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL) + return ; + del(lst->content); + free(lst); +} diff --git a/ft_lstdelone.o b/ft_lstdelone.o new file mode 100644 index 0000000..7e2fa12 Binary files /dev/null and b/ft_lstdelone.o differ diff --git a/ft_lstiter.c b/ft_lstiter.c new file mode 100644 index 0000000..a9b1717 --- /dev/null +++ b/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 18:20:25 by adjoly #+# #+# */ +/* Updated: 2023/11/11 18:23:58 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (lst == NULL || f == NULL) + return ; + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/ft_lstiter.o b/ft_lstiter.o new file mode 100644 index 0000000..12fd286 Binary files /dev/null and b/ft_lstiter.o differ diff --git a/ft_lstlast.c b/ft_lstlast.c new file mode 100644 index 0000000..f5c8f54 --- /dev/null +++ b/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 16:03:12 by adjoly #+# #+# */ +/* Updated: 2023/11/11 16:37:10 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (NULL); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/ft_lstlast.o b/ft_lstlast.o new file mode 100644 index 0000000..74fc159 Binary files /dev/null and b/ft_lstlast.o differ diff --git a/ft_lstmap.c b/ft_lstmap.c new file mode 100644 index 0000000..bb470ed --- /dev/null +++ b/ft_lstmap.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 18:24:49 by adjoly #+# #+# */ +/* Updated: 2023/11/11 18:38:51 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + t_list *tmp; + + if (lst == NULL || f == NULL || del == NULL) + return (NULL); + res = NULL; + while (lst) + { + tmp = ft_lstnew(f(lst->content)); + if (tmp == NULL) + { + ft_lstclear(&tmp, del); + return (NULL); + } + ft_lstadd_back(&res, tmp); + lst = lst->next; + } + return (res); +} diff --git a/ft_lstmap.o b/ft_lstmap.o new file mode 100644 index 0000000..eaf43d2 Binary files /dev/null and b/ft_lstmap.o differ diff --git a/ft_lstnew.c b/ft_lstnew.c new file mode 100644 index 0000000..298e054 --- /dev/null +++ b/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 15:30:32 by adjoly #+# #+# */ +/* Updated: 2023/11/11 15:51:49 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *lst; + + lst = malloc(sizeof(t_list)); + if (lst == NULL) + return (NULL); + lst[0].content = content; + lst[0].next = NULL; + return (lst); +} diff --git a/ft_lstnew.o b/ft_lstnew.o new file mode 100644 index 0000000..79695bb Binary files /dev/null and b/ft_lstnew.o differ diff --git a/ft_lstsize.c b/ft_lstsize.c new file mode 100644 index 0000000..62a77a5 --- /dev/null +++ b/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/11 15:53:01 by adjoly #+# #+# */ +/* Updated: 2023/11/11 15:59:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst != NULL) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/ft_lstsize.o b/ft_lstsize.o new file mode 100644 index 0000000..94fa721 Binary files /dev/null and b/ft_lstsize.o differ diff --git a/libft.h b/libft.h index 37289a5..5d540b8 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ -/* Updated: 2023/11/11 14:22:48 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 18:37:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,12 @@ # include # include +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + int ft_atoi(const char *nptr); void *ft_calloc(size_t nmemb, size_t size); int ft_isalnum(int c); @@ -54,4 +60,14 @@ char **ft_split(char const *s, char c); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); void ft_striteri(char *s, void (*f)(unsigned int, char *)); +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 \ No newline at end of file