-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunks_push_a.c
More file actions
69 lines (63 loc) · 1.44 KB
/
Copy pathchunks_push_a.c
File metadata and controls
69 lines (63 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* chunks_push_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yelallam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/24 02:01:25 by yelallam #+# #+# */
/* Updated: 2026/02/24 10:47:51 by yelallam ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int find_max_index(t_stack *head)
{
int max;
max = INT_MIN;
while (head)
{
if (head -> index > max)
max = head -> index;
head = head -> next;
}
return (max);
}
static int find_pos_by_index(t_stack *head, int max_index)
{
int pos;
pos = 0;
while (head)
{
if (head -> index == max_index)
return (pos);
head = head -> next;
pos++;
}
return (pos);
}
void chunks_push_a(t_stack **head_a, t_stack **head_b)
{
int max_index;
int max_pos;
int size;
int i;
while (*head_b)
{
max_index = find_max_index(*head_b);
max_pos = find_pos_by_index(*head_b, max_index);
size = ft_lstsize(*head_b);
i = 0;
if (max_pos <= size / 2)
{
while (i++ < max_pos)
ft_rotate(head_b, 'b');
}
else
{
i = 0;
while (i++ < size - max_pos)
ft_rev_rotate(head_b, 'b');
}
ft_push_a(head_b, head_a);
}
}