-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlow_size_handler.c
More file actions
94 lines (87 loc) · 2.46 KB
/
low_size_handler.c
File metadata and controls
94 lines (87 loc) · 2.46 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* low_size_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 20:14:27 by uyilmaz #+# #+# */
/* Updated: 2023/03/06 20:14:30 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_list_sorted(t_list *list)
{
t_list *next;
next = list->next;
while (next)
{
if (*(list->content) > *(next->content))
return (0);
list = list->next;
next = next->next;
}
return (1);
}
void list_reducer(t_list *stack_a)
{
int min;
int index;
min = *(stack_a->content);
index = 0;
while (stack_a)
{
if (*(stack_a->content) <= min)
{
min = *(stack_a->content);
t_data.index_a = index;
t_data.target = *(stack_a->content);
}
stack_a = stack_a->next;
index++;
}
if (t_data.index_a > t_data.size_a - t_data.index_a)
t_data.cost_a = -1 * (t_data.size_a - t_data.index_a);
else
t_data.cost_a = t_data.index_a;
prepare_stack_a();
pb(&t_data.stack_a, &t_data.stack_b);
}
void handle_three(int first, int second, int third)
{
while (!is_list_sorted(t_data.stack_a))
{
if (first > second && first > third)
{
ra(&t_data.stack_a);
ft_putstr("ra\n");
}
else if (second > first && second > third)
{
rra(&t_data.stack_a);
ft_putstr("rra\n");
}
else if (third > first && third > second)
sa(&t_data.stack_a);
first = *(t_data.stack_a->content);
second = *(t_data.stack_a->next->content);
third = *(t_data.stack_a->next->next->content);
}
}
void low_size_handler(t_list *stack_a)
{
while (t_data.size_a > 3)
list_reducer(t_data.stack_a);
if (t_data.size_a == 2)
sa(&t_data.stack_a);
if (is_list_sorted(t_data.stack_a))
return ;
else
{
stack_a = t_data.stack_a;
if (is_list_sorted(t_data.stack_a))
return ;
handle_three(*(stack_a->content), *(stack_a->next->content),
*(stack_a->next->next->content));
}
}