-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_utils_link_chunk.c
85 lines (77 loc) · 2.19 KB
/
ft_utils_link_chunk.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_link_chunk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flormich <flormich@student.42wolfsburg.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/01 22:32:12 by flormich #+# #+# */
/* Updated: 2021/08/01 22:41:50 by flormich ### ########.fr */
/* */
/* ************************************************************************** */
#include"push_swap.h"
/*
* Delete all the element after the one pointed by **lst
*/
void ft_lstclear(t_chunk **lst)
{
t_chunk *mem_next;
if (!lst)
return ;
mem_next = (*lst)->next;
while (*lst)
{
mem_next = (*lst)->next;
free(*lst);
*lst = mem_next;
}
*lst = NULL;
}
// Only for the first chunk, NB element = summe
void ft_implemente_chunk(t_chunk *chunk, int nb)
{
if (nb < chunk->min)
chunk->min = nb;
if (nb > chunk->max)
chunk->max = nb;
if (nb < chunk->min_ist)
chunk->min_ist = nb;
if (nb > chunk->max_ist)
chunk->max_ist = nb;
chunk->nb_elt_total = chunk->nb_elt_total + 1;
chunk->sum = chunk->sum + nb;
chunk->average = chunk->sum / chunk->nb_elt_total;
}
void ft_initialise_chunk(t_chunk *chunk, int chk_nr, t_chunk *prev)
{
chunk->chk_nr = chk_nr;
chunk->min = 32768;
chunk->max = -32767;
chunk->min_ist = 32768;
chunk->max_ist = -32767;
chunk->sum = 0;
chunk->average = 0;
chunk->nb_elt_total = 0;
chunk->nb_elt_curent = 0;
chunk->nb_a_rotate = 0;
chunk->nb_b_rotate = 0;
chunk->previous = prev;
chunk->next = NULL;
}
t_chunk *ft_find_last(t_chunk *lst)
{
if (lst)
{
while (lst->next != NULL)
lst = lst->next;
return (lst);
}
return (NULL);
}
// Display error message and free Chunk_Liste
void ft_error(t_chunk *chunk)
{
write(1, "Error\n", 6);
if (chunk)
ft_lstclear(&chunk);
}