diff --git a/Makefile b/Makefile index e0a97bc..76359d5 100644 --- a/Makefile +++ b/Makefile @@ -48,13 +48,13 @@ _SRC += ft_strmapi.c _SRC += ft_lstnew.c _SRC += ft_lstadd_front.c -# _SRC += ft_lstadd_back.c -# _SRC += ft_lstclear -# _SRC += ft_lstdelone.c -# _SRC += ft_lstiter.c -# _SRC += ft_lstlast.c -# _SRC += ft_lstmap.c -# _SRC += ft_lstsize.c +_SRC += ft_lstsize.c +_SRC += ft_lstlast.c +_SRC += ft_lstadd_back.c +_SRC += ft_lstclear.c +_SRC += ft_lstdelone.c +_SRC += ft_lstiter.c +_SRC += ft_lstmap.c _SRC += ft_char_in_charset.c _SRC += ft_isspace.c diff --git a/a.out b/a.out new file mode 100755 index 0000000..899bc18 Binary files /dev/null and b/a.out differ diff --git a/ft_lstadd_back.c b/ft_lstadd_back.c index e69de29..e2cef0e 100644 --- a/ft_lstadd_back.c +++ b/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 16:29:20 by tosuman #+# #+# */ +/* Updated: 2023/06/24 16:38:53 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *last; + + if (!lst) + return ; + if (!*lst) + { + *lst = new; + return ; + } + last = ft_lstlast(*lst); + last->next = new; +} diff --git a/ft_lstadd_front.c b/ft_lstadd_front.c index e8c412f..84ea40e 100644 --- a/ft_lstadd_front.c +++ b/ft_lstadd_front.c @@ -6,7 +6,7 @@ /* By: tosuman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/14 11:57:15 by tosuman #+# #+# */ -/* Updated: 2023/06/14 11:57:15 by tosuman ### ########.fr */ +/* Updated: 2023/06/24 16:38:36 by tosuman ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,8 @@ void ft_lstadd_front(t_list **lst, t_list *new) { - if (lst) - { - if (*lst) - new->next = *lst; - *lst = new; - } + if (!lst) + return ; + new->next = *lst; + *lst = new; } diff --git a/ft_lstclear b/ft_lstclear deleted file mode 100644 index e69de29..0000000 diff --git a/ft_lstclear.c b/ft_lstclear.c new file mode 100644 index 0000000..e593f3b --- /dev/null +++ b/ft_lstclear.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 16:47:41 by tosuman #+# #+# */ +/* Updated: 2023/06/24 17:06:04 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tmp; + + if (!lst || !del) + return ; + tmp = *lst; + while (tmp) + { + *lst = tmp->next; + ft_lstdelone(tmp, del); + tmp = *lst; + } +} diff --git a/ft_lstdelone.c b/ft_lstdelone.c index e69de29..c1f1ba4 100644 --- a/ft_lstdelone.c +++ b/ft_lstdelone.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 16:44:13 by tosuman #+# #+# */ +/* Updated: 2023/06/24 16:45:34 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + del(lst->content); + free(lst); +} diff --git a/ft_lstiter.c b/ft_lstiter.c index e69de29..472aa88 100644 --- a/ft_lstiter.c +++ b/ft_lstiter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 16:57:28 by tosuman #+# #+# */ +/* Updated: 2023/06/24 17:26:38 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + (*f)(lst->content); + lst = lst->next; + } +} diff --git a/ft_lstlast.c b/ft_lstlast.c index e69de29..35b8ce5 100644 --- a/ft_lstlast.c +++ b/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 16:26:09 by tosuman #+# #+# */ +/* Updated: 2023/06/24 16:28:25 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (!lst) + return (lst); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/ft_lstmap.c b/ft_lstmap.c index e69de29..2f071b3 100644 --- a/ft_lstmap.c +++ b/ft_lstmap.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 17:10:04 by tosuman #+# #+# */ +/* Updated: 2023/06/26 20:32:34 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_lst; + t_list *new_elem; + + if (!lst || !f) + return (0); + new_lst = (void *)0; + while (lst) + { + new_elem = ft_lstnew(f(lst->content)); + if (!new_elem) + { + while (new_lst) + { + new_elem = new_lst->next; + del(new_lst->content); + free(new_lst); + new_lst = new_elem; + } + return (0); + } + ft_lstadd_back(&new_lst, new_elem); + lst = lst->next; + } + return (new_lst); +} diff --git a/ft_lstnew.c b/ft_lstnew.c index e017942..2b8b267 100644 --- a/ft_lstnew.c +++ b/ft_lstnew.c @@ -6,12 +6,13 @@ /* By: tosuman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/09 14:55:31 by tosuman #+# #+# */ -/* Updated: 2023/06/09 14:55:31 by tosuman ### ########.fr */ +/* Updated: 2023/06/24 17:36:37 by tosuman ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include +#include t_list *ft_lstnew(void *content) { @@ -21,6 +22,6 @@ t_list *ft_lstnew(void *content) if (!new) return (0); new->content = content; - new->next = 0; + new->next = (void *)0; return (new); } diff --git a/ft_lstsize.c b/ft_lstsize.c index e69de29..d551feb 100644 --- a/ft_lstsize.c +++ b/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tosuman +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/24 15:46:41 by tosuman #+# #+# */ +/* Updated: 2023/06/24 16:25:28 by tosuman ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst) + { + lst = lst -> next; + i++; + } + return (i); +} diff --git a/ft_strlcat.c b/ft_strlcat.c index 0746d67..bf3d910 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -6,7 +6,7 @@ /* By: tosuman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/11 16:55:00 by tosuman #+# #+# */ -/* Updated: 2023/06/02 20:02:14 by tischmid ### ########.fr */ +/* Updated: 2023/06/26 20:41:08 by tosuman ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,10 @@ size_t ft_strlcat(char *dst, char const *src, size_t size) while (src[slen]) ++slen; idx = 0; - while (dst[idx]) + while (dst[idx] && idx < size) ++idx; dlen = idx; - if (dlen > size) + if (dlen == size) return (slen + size); sidx = 0; size -= dlen; @@ -35,31 +35,3 @@ size_t ft_strlcat(char *dst, char const *src, size_t size) dst[idx] = 0; return (dlen + slen); } - -/* -Alternative implementation -size_t ft_strlcat(char *dst, char const *src, size_t size) -{ - size_t dst_len; - size_t src_len; - - if ((!dst || !src) && size == 0) - return (0); - dst_len = 0; - src_len = -1; - while (*dst++) - ++dst_len; - while (src[++src_len]) - ; - if (size <= dst_len) - return (size + src_len); - if (--dst && dst_len > size) - return (size + dst_len); - while (src_len-- && ++dst_len < size) - *dst++ = *src++; - if (dst_len-- == size) - --dst_len; - *dst = 0; - return (dst_len + src_len); -} -*/ diff --git a/libft.h b/libft.h index 4a8eb3f..88e7679 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: tosuman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/11 11:31:44 by tosuman #+# #+# */ -/* Updated: 2023/06/02 21:25:33 by tischmid ### ########.fr */ +/* Updated: 2023/06/24 17:16:29 by tosuman ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,5 +61,12 @@ int ft_char_in_charset(char c, char const *charset); UINT ft_abs(int n); 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