-
Notifications
You must be signed in to change notification settings - Fork 2
/
generic_list.c
97 lines (86 loc) · 2.01 KB
/
generic_list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* generic_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 16:50:49 by jkong #+# #+# */
/* Updated: 2022/06/22 14:08:08 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "generic_list.h"
int list_walk(t_generic_list *list, t_gl_func *func)
{
t_generic_list *next;
int res;
res = 0;
while (list)
{
next = list->next;
res = (*func)(list);
if (res < 0)
break ;
list = next;
}
return (res);
}
t_generic_list *list_reverse(t_generic_list *list)
{
t_generic_list *next;
t_generic_list *reverse_head;
reverse_head = NULL;
while (list)
{
next = list->next;
list->next = reverse_head;
reverse_head = list;
list = next;
}
return (reverse_head);
}
size_t list_length(t_generic_list *list)
{
size_t i;
i = 0;
while (list)
{
i++;
list = list->next;
}
return (i);
}
void list_append(t_generic_list **head, t_generic_list *elem)
{
t_generic_list *t;
if (!*head)
*head = elem;
else if (elem)
{
t = *head;
while (t->next)
t = t->next;
t->next = elem;
}
}
t_generic_list *list_remove(t_generic_list **list, t_gl_func *cmp, void *arg)
{
t_generic_list *prev;
t_generic_list *t;
prev = NULL;
t = *list;
while (t)
{
if ((*cmp)(t, arg) == 0)
{
if (prev)
prev->next = t->next;
else
*list = t->next;
break ;
}
prev = t;
t = t->next;
}
return (t);
}