-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstnew.c
52 lines (43 loc) · 1.4 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
/***
* Allocates (with malloc(3)) and returns a new element.
* The variable ’content’ is initialized with the value of the
* parameter ’content’. The variable ’next’ is initialized to NULL.
* ---------------------------------------------------------------
* So here we will allocate memory for the new list, aopy the (content)
* to it, If the memory allocation fails then we will return NULL.
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
/***
* • content : The data contained in the element. The void * allows to store any kind
of data.
* • next : The next element’s address or NULL if it’s the last element.
*/
/*--------------------------Required function----------------------------*/
t_list *ft_lstnew(void *content)
{
t_list *new_list;
new_list = (t_list *)malloc(sizeof(t_list));
if (!new_list)
{
return (NULL);
}
/* Initialize The variable ’content’ with the value of the parameter ’content’*/
new_list->content = content;
/* The variable ’next’ is initialized to NULL*/
new_list->next = NULL;
return (new_list);
}
/*------------------------------________----------------------------------*/
/*-----------------------------|Example|-----------------------------------*/
int main()
{
printf("%s\n", (char *)(ft_lstnew("Ghaith") -> content));
return (0);
}