-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_utils.c
82 lines (72 loc) · 1.75 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
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ikayacio <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 11:17:11 by ikayacio #+# #+# */
/* Updated: 2023/06/20 11:18:06 by ikayacio ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *ft_lstnew(long content)
{
t_list *new;
new = (t_list *)malloc(sizeof(t_list));
if (!new)
return (NULL);
new -> next = NULL;
new -> content = content;
new -> checked = 0;
return (new);
}
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (0);
while (lst != NULL)
{
if (lst -> next == NULL)
return (lst);
lst = lst -> next;
}
return (lst);
}
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (lst == NULL || new == NULL)
return ;
if (*lst == NULL)
*lst = new;
else
{
last = ft_lstlast(*lst);
last -> next = new;
}
}
int ft_lstsize(t_list *lst)
{
int size;
size = 0;
while (lst != NULL)
{
lst = lst -> next;
size++;
}
return (size);
}
void ft_lstclear(t_list **lst)
{
t_list *temp;
if (!lst)
return ;
while (*lst != NULL)
{
temp = (*lst)-> next;
free(*lst);
(*lst) = temp;
}
*lst = NULL;
}