-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_list.c
More file actions
83 lines (73 loc) · 1.81 KB
/
utils_list.c
File metadata and controls
83 lines (73 loc) · 1.81 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 10:03:31 by uyilmaz #+# #+# */
/* Updated: 2023/02/27 10:03:32 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_lstdelone(t_list **lst)
{
if (!lst)
return ;
free((*lst)->content);
free(*lst);
}
void ft_lstclear(t_list **lst)
{
t_list *tmp;
if (!lst)
return ;
while (*lst != NULL)
{
tmp = (*lst)->next;
ft_lstdelone(lst);
*lst = tmp;
}
}
t_list *ft_lstnew(int *content)
{
t_list *new_element;
new_element = malloc(sizeof(t_list));
if (!new_element)
return (NULL);
new_element->content = content;
new_element->next = NULL;
return (new_element);
}
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (*lst == NULL)
{
*lst = new;
return ;
}
last = *lst;
while (last->next != NULL)
last = last->next;
last->next = new;
}
t_list *list_initializer(int size, int **arr)
{
t_list *lst;
t_list *new;
int i;
i = 0;
lst = NULL;
while (i < size)
{
new = ft_lstnew(arr[i++]);
if (!new)
{
ft_lstclear(&lst);
return (NULL);
}
ft_lstadd_back(&lst, new);
}
return (lst);
}