-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunks_utils.c
More file actions
91 lines (83 loc) · 2.06 KB
/
Copy pathchunks_utils.c
File metadata and controls
91 lines (83 loc) · 2.06 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* chunks_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: scamlett <scamlett@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/14 17:54:54 by lupalomi #+# #+# */
/* Updated: 2026/05/21 19:29:37 by scamlett ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void assign_chunks_indexes(t_stack *stack)
{
t_node *current;
t_node *compare;
int index;
current = stack->head;
while (current)
{
index = 0;
compare = stack->head;
while (compare)
{
if (compare->value < current->value)
index++;
compare = compare->next;
}
current->index = index;
current = current->next;
}
}
static int get_maximum_position(t_stack *stack)
{
t_node *current;
unsigned int maximum;
unsigned int position;
unsigned int max_pos;
current = stack->head;
maximum = current->index;
position = 0;
max_pos = 0;
while (current)
{
if (current->index > maximum)
{
maximum = current->index;
max_pos = position;
}
current = current->next;
position++;
}
return (max_pos);
}
static void move_maximum_to_head(t_stack *stack)
{
unsigned int position;
position = get_maximum_position(stack);
if (position <= stack->size / 2)
{
while (position > 0)
{
rb(stack);
position--;
}
}
else
{
while (position < stack->size)
{
rrb(stack);
position++;
}
}
}
void push_chunks_to_a(t_stack *stack_a, t_stack *stack_b)
{
while (stack_b->size > 0)
{
move_maximum_to_head(stack_b);
pa(stack_a, stack_b);
}
}