-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstnew.c
47 lines (45 loc) · 1.98 KB
/
ft_lstnew.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oostapen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 17:23:35 by oostapen #+# #+# */
/* Updated: 2024/04/15 17:27:43 by oostapen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* The `ft_lstnew` function creates a new list element of type `t_list`.
*
* Here's how it works:
*
* 1. The function takes one argument: a pointer to `content` that will be
* stored in the new list element.
*
* 2. Then the function allocates memory for the new list element using the
* `malloc` function. The size of the memory allocated is equal to the size
* of the `t_list` structure.
*
* 3. If `malloc` returns `NULL` (which means that the memory was not
* successfully allocated), the function returns `NULL`.
*
* 4. If the memory was successfully allocated, the function sets the
* `content` field of the new list element to the passed `content` argument.
*
* 5. Then the function sets the `next` field of the new list element to
* `NULL`, indicating that this element is the last in the list.
*
* 6. Finally, the function returns a pointer to the new list element.
*/
t_list *ft_lstnew(void *content)
{
t_list *new_node;
new_node = malloc(sizeof(t_list));
if (new_node == NULL)
return (NULL);
new_node->content = content;
new_node->next = NULL;
return (new_node);
}