-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstadd_back.c
69 lines (57 loc) · 2.19 KB
/
ft_lstadd_back.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/08 20:25:27 by mbrito-p #+# #+# */
/* Updated: 2023/05/08 20:25:27 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */
// Adds the node ’new’ at the end of the list.
// lst: The address of a pointer to the first link of
// a list.
// new: The address of a pointer to the node to be
// added to the list.
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *temp;
// Check if the new node or the list itself is NULL
if (!new || !lst)
return ;
// Check if the list is empty
if (!*lst)
{
*lst = new; // If the list is empty, set the new node as the first node
return ;
}
// Traverse the list to the last node
temp = ft_lstlast(*lst);
// Add the new node to the end of the list
temp->next = new;
}
// int main(void)
// {
// t_list *list = NULL;
// t_list *new_node;
// t_list *last;
// // Create the first node of the list
// list = ft_lstnew("Node 1");
// // Add a new node to the front of the list
// new_node = ft_lstnew("Node 2");
// ft_lstadd_front(&list, new_node);
// // Add another node to the front of the list
// new_node = ft_lstnew("Node 3");
// ft_lstadd_front(&list, new_node);
// // Create node and add to the end of the list
// last = ft_lstnew("Node 4");
// ft_lstadd_back(&list, last);
// while (list)
// {
// printf("%s\n", (char *)list->content);
// list = list->next;
// }
// return (0);
// }