ft_lstadd_back: initial implementation
This commit is contained in:
parent
4709b5929a
commit
c3a67f660b
3 changed files with 61 additions and 2 deletions
5
Makefile
5
Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
|
||||
# Updated: 2024/10/21 12:06:41 by kcolin ### ########.fr #
|
||||
# Updated: 2024/10/21 13:08:04 by kcolin ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
|
@ -50,7 +50,8 @@ OBJECTS = $(SOURCES:.c=.o)
|
|||
BONUS_SOURCES = ft_lstnew_bonus.c \
|
||||
ft_lstadd_front_bonus.c \
|
||||
ft_lstsize_bonus.c \
|
||||
ft_lstlast_bonus.c
|
||||
ft_lstlast_bonus.c \
|
||||
ft_lstadd_back_bonus.c
|
||||
BONUS_OBJECTS = $(BONUS_SOURCES:.c=.o)
|
||||
|
||||
.PHONY: all
|
||||
|
|
|
|||
57
ft_lstadd_back_bonus.c
Normal file
57
ft_lstadd_back_bonus.c
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_back_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/21 12:13:37 by kcolin #+# #+# */
|
||||
/* Updated: 2024/10/21 13:29:52 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include <stdio.h> // bad
|
||||
|
||||
void ft_lstadd_back(t_list **lst, t_list *new)
|
||||
{
|
||||
t_list *back;
|
||||
|
||||
if (*lst == NULL)
|
||||
*lst = new;
|
||||
else
|
||||
{
|
||||
back = ft_lstlast(*lst);
|
||||
if (back != NULL)
|
||||
back->next = new;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
t_list *list;
|
||||
t_list *new;
|
||||
|
||||
list = NULL;
|
||||
|
||||
|
||||
ft_lstadd_back(&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_back(&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));
|
||||
free(list->next);
|
||||
free(list);
|
||||
return (0);
|
||||
}
|
||||
*/
|
||||
1
libft.h
1
libft.h
|
|
@ -73,5 +73,6 @@ 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);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue