-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
96 lines (87 loc) · 1.82 KB
/
helpers.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
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fraqioui <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 08:06:21 by fraqioui #+# #+# */
/* Updated: 2022/12/24 18:39:52 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_is_odd(int len)
{
return (len % 2 == 1);
}
static void ft_odd(t_node *list, int len)
{
int i;
i = 0;
while (i <= len / 2)
{
list = list->next;
list->index = i;
i++;
}
i--;
while (i >= 1)
{
list = list->next;
list->index = -i;
i--;
}
}
static void ft_even(t_node *list, int len)
{
int i;
i = 0;
while (i < len / 2)
{
list = list->next;
list->index = i;
i++;
}
while (i >= 1)
{
list = list->next;
list->index = -i;
i--;
}
}
void ft_indexing(t_node *list, int len)
{
int i;
i = 0;
if (!list)
return ;
if (ft_is_odd(len))
ft_odd(list, len);
else
ft_even(list, len);
}
void ft_into_action(t_node **list, int index)
{
int i;
i = 0;
if (!list || !*list)
return ;
if (index < 0)
{
while (i < -1 * index)
{
ft_rev_rotate_a(list);
write(1, "rra\n", 4);
i++;
}
}
else
{
while (i < index)
{
ft_rotate_a(list);
write(1, "ra\n", 3);
i++;
}
}
}