-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_three.c
77 lines (73 loc) · 1.93 KB
/
algo_three.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo_three.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fraqioui <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 08:08:44 by fraqioui #+# #+# */
/* Updated: 2022/12/24 18:32:33 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void ft_helper_1(t_node **a, int max, int min)
{
if (max == (*a)->data)
{
ft_swap_a(*a);
write(1, "sa\n", 3);
}
else if (min == (*a)->data)
{
if (max == (*a)->next->data)
{
ft_swap_a(*a);
write(1, "sa\n", 3);
ft_rev_rotate_a(a);
write(1, "rra\n", 4);
}
else
{
ft_rev_rotate_a(a);
write(1, "rra\n", 4);
}
}
else
return ;
}
static void ft_helper_2(t_node **a, int max, int min)
{
if (min == (*a)->next->data && max == (*a)->prev->data)
{
ft_swap_a(*a);
write(1, "sa\n", 3);
ft_rotate_a(a);
write(1, "ra\n", 3);
}
else if (min == (*a)->prev->data && max == (*a)->next->data)
{
ft_rotate_a(a);
write(1, "ra\n", 3);
}
}
void ft_algo_of_three(t_node **stack_a, int len)
{
int max;
int min;
if (!stack_a || !*stack_a)
return ;
max = ft_max_data(*stack_a, len);
min = ft_min_data(*stack_a, len);
if (ft_sorted(*stack_a, len) == 1)
return ;
if (len == 3)
{
ft_helper_1(stack_a, max, min);
ft_helper_2(stack_a, max, min);
}
else
{
ft_swap_a(*stack_a);
write(1, "sa\n", 3);
}
}