-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_a.c
46 lines (43 loc) · 1.71 KB
/
index_a.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* index_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/29 18:17:03 by yorlians #+# #+# */
/* Updated: 2023/05/31 08:08:59 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*
calculate the number of rotations needed to bring specific element from
stack_b to stack_b to stack_a. by iterating trough stack_a check if the
current element is greater than the number at the top of stack_b or is
less than the current next_num. if so update next_num. move to next element.
*/
int find_rotations_a(t_stack *stack_a, t_stack *stack_b)
{
int index;
int i;
int next_num;
t_stack *temp;
i = 0;
index = 0;
next_num = stack_b -> number;
temp = stack_a;
while (temp != NULL)
{
if (temp -> number > stack_b -> number && \
(temp -> number < next_num || next_num == stack_b -> number))
{
next_num = temp -> number;
index = i;
}
i++;
temp = temp -> next;
}
if (next_num == stack_b -> number || next_num == find_min(stack_a))
return (find_index_of_min(stack_a));
return (index);
}