-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_back_a.c
65 lines (60 loc) · 2.09 KB
/
push_back_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_back_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yorlians <yorlians@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/31 03:11:56 by yorlians #+# #+# */
/* Updated: 2023/05/31 08:13:44 by yorlians ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*
move all elements from stack_b to stack_a in an optimal way.
*/
void put_all_back(t_stack **stack_a, t_stack **stack_b)
{
int moves;
t_ab rot;
t_ab size;
t_stack *temp_b;
moves = -1;
rot.a = 0;
rot.b = 0;
size.a = stack_size(*stack_a);
size.b = stack_size(*stack_b);
temp_b = *stack_b;
rot = find_moves(*stack_a, temp_b, size);
do_double_rot(stack_a, stack_b, &rot.a, &rot.b);
do_single_rot(stack_a, stack_b, rot.a, rot.b);
push_a(stack_a, stack_b, 1);
}
/*
rearrange the elements and return them to stack_a while
maintaining a specific order, such as median value being
at the top.
*/
void put_all_back_median(t_stack **stack_a, t_stack **stack_b)
{
int moves;
t_ab rotations;
t_ab temp_rotations;
t_stack *temp_b;
moves = -1;
rotations.a = 0;
rotations.b = 0;
temp_rotations.b = 0;
temp_b = *stack_b;
temp_b = *stack_b;
while (temp_b)
{
temp_rotations.a = find_rotations_a(*stack_a, temp_b);
if (temp_rotations.a + temp_rotations.b < moves || moves == -1)
moves = save_moves(&rotations, temp_rotations.a, temp_rotations.b);
temp_rotations.b++;
temp_b = temp_b -> next;
}
rot_no_print(stack_a, stack_b, rotations.a, rotations.b);
push_a(stack_a, stack_b, 0);
}