-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstadd_back_bonus.c
45 lines (41 loc) · 1.8 KB
/
ft_lstadd_back_bonus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadewumi <oadewumi@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 14:18:18 by oadewumi #+# #+# */
/* Updated: 2023/12/13 11:37:42 by oadewumi ### ########.fr */
/* */
/* ************************************************************************** */
/* This function adds a new node (t_list *new) to the end of the list
that t_list **lst is pointing to. */
/* To do this, I understood that moving from the first node to the next could
lead to losing the head (first node), hence, we create a new t_list *extra to
hold the position and content of dereferenced (**lst), so to *lst. */
/* However, we need to put some conditions in place:
. If *lst is NULL, then *lst is equal 'new'. and the program returns (ends).
. If new exists, then run the function in a loop with 'extra' as the new head
pointing to the next node until the last node. Then equate the last node ref
to 'new'. */
/* Since its a void function. it has no return value. */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *extra;
extra = *lst;
if (*lst == NULL)
{
*lst = new;
return ;
}
if (new)
{
while (extra -> next != NULL)
{
extra = extra -> next;
}
extra -> next = new;
}
}