-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_utils.c
72 lines (63 loc) · 1.65 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ltombell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 11:34:23 by ltombell #+# #+# */
/* Updated: 2022/12/10 10:36:31 by ltombell ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_lista *create_elem(int n)
{
t_lista *elem;
elem = (t_lista *)malloc(sizeof(t_lista));
elem->nb = n;
elem->next = NULL;
elem->prev = NULL;
return (elem);
}
void ft_add_element(t_lista **lista, int n)
{
t_lista *new;
t_lista *tmp;
if (!*lista)
*lista = create_elem(n);
else
{
tmp = *lista;
while (tmp->next)
tmp = tmp->next;
new = create_elem(n);
tmp->next = new;
new->prev = tmp;
}
}
int ft_list_length(t_lista *lista)
{
t_lista *tmp;
int i;
i = 0;
tmp = lista;
while (tmp)
{
tmp = tmp->next;
i++;
}
return (i);
}
void ft_add_element_to_start(t_lista **lista, int nb)
{
t_lista *new;
new = create_elem(nb);
if (!*lista)
*lista = new;
else
{
new->next = (*lista);
(*lista)->prev = new;
*lista = new;
}
}