-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils2.c
108 lines (99 loc) · 2.93 KB
/
utils2.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
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joalmeid <joalmeid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 10:01:13 by joalmeid #+# #+# */
/* Updated: 2023/03/08 19:22:02 by joalmeid ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int find_stack_position(int index, t_list *node)
{
int position;
t_list *head;
head = node;
position = 1;
while (node->next != NULL)
{
if (node->index == index)
return (position);
node = node->next;
position ++;
}
if ((position == ft_lstsize(head)) \
&& ((ft_lstlast(head))->index != index))
position = 0;
return (position);
}
int find_minor_position(t_list *node)
{
int position;
int minor;
t_list *head;
head = node;
position = 0;
minor = node->index;
node = node->next;
if (node == NULL)
return (-1);
while (node != NULL)
{
if (minor > node->index)
minor = node->index;
node = node->next;
}
position = find_stack_position(minor, head);
return (position);
}
t_push set_push_stategy(t_list *stack, int init, int end, int stack_size)
{
int first_qnt;
int second_qnt;
int first_p;
t_push set;
first_qnt = 0;
second_qnt = 0;
first_p = ft_lstsize(stack) / 2 + ft_lstsize(stack) % 2;
set.init = init;
set.end = end;
set.top_or_botton = 1;
set.size = stack_size;
while (stack)
{
if (stack->index >= init && stack->index <= end && first_p > 0)
first_qnt ++;
else if (stack->index >= init && stack->index <= end && first_p <= 0)
second_qnt ++;
first_p --;
stack = stack->next;
}
set.qnt = first_qnt + second_qnt;
if (first_qnt < second_qnt)
set.top_or_botton = 2;
return (set);
}
void optmize_b(t_list **stack_a, t_list **stack_b, t_push set)
{
if (*stack_b && (*stack_b)->index < ft_lstlast((*stack_b))->index \
&& (ft_lstlast((*stack_b))->index >= set.init \
&& ft_lstlast((*stack_b))->index <= set.end))
{
if (*stack_a && (*stack_a)->index > ft_lstlast((*stack_a))->index)
rrr(stack_a, stack_b);
}
}
void optmize_push_b(t_list **stack_a, t_list **stack_b, t_push set)
{
while (*stack_a && (*stack_a)->index > ft_lstlast((*stack_a))->index \
&& (ft_lstlast((*stack_a))->index >= set.init \
&& ft_lstlast((*stack_a))->index <= set.end))
{
if (*stack_b && (*stack_b)->index < (ft_lstlast(*stack_b))->index)
rrr(stack_a, stack_b);
else
rra(stack_a);
}
}