-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstnew.c
64 lines (59 loc) · 2.26 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbutt <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/22 10:28:11 by mbutt #+# #+# */
/* Updated: 2019/05/08 19:47:50 by mbutt ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Description- Allocates (with malloc) and returns a "fresh" link. The
** variables content and content_size of the new link are initialized by copy of
** the parameters of the function. If the parameter content is nul, the
** variable content is initialized to NULL and the variable content_size is
** initialized to 0 even if the parameter content_size isnt. The variable next
** is initialized to NULL. If the allocation fails, the function returns NULL.
** Param#1- The content to put in the new link.
** Param#2- The size of the cotent of the new link.
** RETURN VALUE- The new link.
** Libc functions - malloc, free.
** -> A linked list is a data structure.
*/
#include "libft.h"
t_list *ft_lstnew(void const *content, size_t content_size)
{
t_list *n_node;
n_node = (t_list *)malloc(sizeof(t_list));
if (n_node == NULL)
return (NULL);
if (content == 0)
{
n_node->content = NULL;
n_node->content_size = 0;
return (n_node);
}
n_node->content = malloc(sizeof(void) * (content_size));
if (!(n_node->content))
return (NULL);
ft_memcpy(n_node->content, content, content_size);
n_node->content_size = content_size;
n_node->next = NULL;
return (n_node);
}
/*
int main (void)
{
t_list *n_node2;
void const *string;
size_t size;
string = "This is a test\n";
size = 16;
n_node2 = ft_lstnew(string, size);
printf("%s", n_node2->content);
printf("%zu", n_node2->content_size);
return(0);
}
*/