-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_utils.c
76 lines (67 loc) · 1.72 KB
/
list_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agarijo- <agarijo-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/22 15:26:23 by agarijo- #+# #+# */
/* Updated: 2023/04/11 16:17:31 by agarijo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_node *lst_new_node(void)
{
t_node *new_node;
new_node = (t_node *)malloc(sizeof(t_node));
if (!new_node)
return (NULL);
new_node->next = NULL;
new_node->rank = -1;
new_node->position = -1;
new_node->targ_position = -1;
return (new_node);
}
void lst_add_back(t_node **lst, t_node *new)
{
if (!*lst)
*lst = new;
else
{
lst_last(*lst)->next = new;
}
}
t_node *lst_second_to_last(t_node *lst)
{
t_node *second_to_last;
second_to_last = NULL;
while (lst)
{
if (!(lst->next))
return (second_to_last);
second_to_last = lst;
lst = lst->next;
}
return (second_to_last);
}
t_node *lst_last(t_node *lst)
{
while (lst)
{
if (!(lst->next))
return (lst);
lst = lst->next;
}
return (lst);
}
int lst_size(t_node *lst)
{
int count;
count = 0;
while (lst)
{
lst = lst->next;
count++;
}
return (count);
}