From a872beadb998e8f37b9f541932c45c17c2101a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 21 Oct 2024 12:12:35 +0200 Subject: [PATCH] ft_lstlast: initial implementation --- Makefile | 5 +++-- ft_lstlast_bonus.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 3 ++- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 ft_lstlast_bonus.c diff --git a/Makefile b/Makefile index 71f1c5c..065768c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/21 11:49:58 by kcolin ### ########.fr # +# Updated: 2024/10/21 12:06:41 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -49,7 +49,8 @@ SOURCES = ft_isalpha.c \ OBJECTS = $(SOURCES:.c=.o) BONUS_SOURCES = ft_lstnew_bonus.c \ ft_lstadd_front_bonus.c \ - ft_lstsize_bonus.c + ft_lstsize_bonus.c \ + ft_lstlast_bonus.c BONUS_OBJECTS = $(BONUS_SOURCES:.c=.o) .PHONY: all diff --git a/ft_lstlast_bonus.c b/ft_lstlast_bonus.c new file mode 100644 index 0000000..a53955f --- /dev/null +++ b/ft_lstlast_bonus.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/21 12:03:16 by kcolin #+# #+# */ +/* Updated: 2024/10/21 12:04:50 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + t_list *current; + + current = lst; + while (current->next != NULL) + { + current = current->next; + } + return (current); +} + +/* +#include +#include + +int main(void) +{ + t_list *list; + t_list *new; + + list = ft_lstnew("Hello There!"); + printf("current:\t%p\n", list); + printf("content:\t%s\n", (char *)list->content); + printf("next:\t\t%p\n", list->next); + printf("length:\t%d\n", ft_lstsize(list)); + new = ft_lstnew("New Element!"); + ft_lstadd_front(&list, new); + printf("current:\t%p\n", list); + printf("content:\t%s\n", (char *)list->content); + printf("next:\t\t%p\n", list->next); + printf("length:\t%d\n", ft_lstsize(list)); + printf("last:\t\t%p\n", ft_lstlast(list)); + free(list->next); + free(list); + return (0); +} +*/ diff --git a/libft.h b/libft.h index f44244a..1ef0af2 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/21 11:11:02 by kcolin ### ########.fr */ +/* Updated: 2024/10/21 12:11:28 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,5 +72,6 @@ typedef struct s_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); #endif