-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_seperate_rotations.c
83 lines (74 loc) · 2.54 KB
/
calculate_seperate_rotations.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* calculate_seperate_rotations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shaas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 22:18:41 by shaas #+# #+# */
/* Updated: 2022/03/09 17:57:45 by shaas ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/*the way this works is, we try to find the next biggest number in stack_a
to the number we want to push from stack b.
but, if the number is bigger than all the numbers on a, we can't find anything.
so, while iterating through the list, we also find the smallest number. and
if our number is the biggest number, the smallest is "equal" to next biggest.*/
static t_node *find_bottom(t_list *stack_a, t_node *node)
{
t_node *iter;
t_node *bottom;
t_node *smallest;
iter = stack_a->head;
bottom = NULL;
smallest = stack_a->head;
while (iter != NULL)
{
if ((bottom == NULL && iter->rank > node->rank) || \
(iter->rank > node->rank && iter->rank < bottom->rank))
bottom = iter;
if (iter->rank < smallest->rank)
smallest = iter;
iter = iter->next;
}
if (bottom == NULL)
bottom = smallest;
return (bottom);
}
unsigned int calculate_rotate(t_node *node, t_list *stack)
{
t_node *iter;
unsigned int rotate;
iter = node;
rotate = 0;
while (iter != stack->head)
{
iter = iter->prev;
rotate++;
}
return (rotate);
}
unsigned int calculate_reverse_rotate(t_node *node, t_list *stack)
{
t_node *iter;
unsigned int reverse_rotate;
iter = node;
reverse_rotate = 0;
while (iter != NULL && iter != stack->head)
{
iter = iter->next;
reverse_rotate++;
}
return (reverse_rotate);
}
void calculate_seperate_rotations(\
t_node *node, t_sort *sort, t_list *stack_a, t_list *stack_b)
{
t_node *bottom;
bottom = find_bottom(stack_a, node);
sort->rb = calculate_rotate(node, stack_b);
sort->rrb = calculate_reverse_rotate(node, stack_b);
sort->ra = calculate_rotate(bottom, stack_a);
sort->rra = calculate_reverse_rotate(bottom, stack_a);
}