-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.c
96 lines (87 loc) · 2.17 KB
/
position.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* position.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <erigolon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/05 13:40:15 by erigolon #+# #+# */
/* Updated: 2023/06/12 14:57:24 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
static void position(t_stack **stack)
{
t_stack *temp;
int i;
temp = *stack;
i = 0;
while (temp)
{
temp->pos = i;
temp = temp->next;
i++;
}
}
int index_low_pos(t_stack **stack)
{
t_stack *temp;
int low_index;
int low_pos;
temp = *stack;
low_index = INT_MAX;
position(stack);
low_pos = temp->pos;
while (temp)
{
if (temp->index < low_index)
{
low_index = temp->index;
low_pos = temp->pos;
}
temp = temp->next;
}
return (low_pos);
}
static int get_target(t_stack **a, int b_idx, int target_idx, int target_pos)
{
t_stack *tmp_a;
tmp_a = *a;
while (tmp_a)
{
if (tmp_a->index > b_idx && tmp_a->index < target_idx)
{
target_idx = tmp_a->index;
target_pos = tmp_a->pos;
}
tmp_a = tmp_a->next;
}
if (target_idx != INT_MAX)
return (target_pos);
tmp_a = *a;
while (tmp_a)
{
if (tmp_a->index < target_idx)
{
target_idx = tmp_a->index;
target_pos = tmp_a->pos;
}
tmp_a = tmp_a->next;
}
return (target_pos);
}
void pos_target(t_stack **stack_a, t_stack **stack_b)
{
t_stack *temp;
int target_pos;
temp = *stack_b;
position(stack_a);
position(stack_b);
target_pos = 0;
while (temp)
{
target_pos = get_target(stack_a, temp->index, INT_MAX, target_pos);
temp->target_pos = target_pos;
temp = temp->next;
}
}