-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_sorty.c
69 lines (63 loc) · 1.97 KB
/
ft_sorty.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sorty.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marias-e <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 15:56:10 by marias-e #+# #+# */
/* Updated: 2023/01/19 17:33:12 by marias-e ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_big_a(t_list **stack_a, t_list **mov, size_t *num)
{
if (num[1] < num[2])
{
ft_rotate(stack_a);
ft_lstadd_back(mov, ft_lstnew(ra));
}
else
{
ft_rotate(stack_a);
ft_lstadd_back(mov, ft_lstnew(ra));
ft_swap(stack_a);
ft_lstadd_back(mov, ft_lstnew(sa));
}
}
static void ft_big_b(t_list **stack_a, t_list **mov, size_t *num)
{
if (num[0] > num[2])
{
ft_rev_rotate(stack_a);
ft_lstadd_back(mov, ft_lstnew(rra));
}
else
{
ft_rev_rotate(stack_a);
ft_lstadd_back(mov, ft_lstnew(rra));
ft_swap(stack_a);
ft_lstadd_back(mov, ft_lstnew(sa));
}
}
static void ft_big_c(t_list **stack_a, t_list **mov, size_t *num)
{
if (num[0] > num[1])
{
ft_swap(stack_a);
ft_lstadd_back(mov, ft_lstnew(sa));
}
}
void ft_sorty(t_list **stack_a, t_list **mov)
{
size_t num[3];
num[0] = (*stack_a)->index;
num[1] = (*stack_a)->next->index;
num[2] = (*stack_a)->next->next->index;
if (num[0] > num[1] && num[0] > num[2])
ft_big_a(stack_a, mov, num);
else if (num[1] > num[0] && num[1] > num[2])
ft_big_b(stack_a, mov, num);
else
ft_big_c(stack_a, mov, num);
}