-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.c
55 lines (46 loc) · 938 Bytes
/
linkedlist.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
/*
** EPITECH PROJECT, 2018
** linked
** File description:
** linked
*/
#include "server.h"
t_node *createt_node(void *data)
{
t_node *newt_node = malloc(sizeof(t_node));
if (newt_node == NULL)
return (fprintf(stderr, "Out of memory.\n"), NULL);
newt_node->data = data;
newt_node->next = NULL;
return (newt_node);
}
t_list *emptyt_list(void)
{
t_list *list = malloc(sizeof(t_list));
if (list == NULL)
return (fprintf(stderr, "Out of memory.\n"), NULL);
list->head = NULL;
return (list);
}
void add(void *data, t_list *list)
{
t_node *current = NULL;
if (list->head == NULL) {
list->head = createt_node(data);
} else {
current = list->head;
while (current->next != NULL)
current = current->next;
current->next = createt_node(data);
}
}
size_t count(t_list *list)
{
size_t size = 0;
t_node *current = list->head;
while (current != NULL) {
size += 1;
current = current->next;
}
return (size);
}