-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap_utils.c
executable file
·101 lines (90 loc) · 2.33 KB
/
push_swap_utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hameur <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 16:55:53 by hameur #+# #+# */
/* Updated: 2022/06/26 23:40:37 by hameur ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void push_int(t_stack **s_a, t_stack **s_b, int push, int size)
{
t_stack *first;
int i;
int j;
i = -1;
j = 0;
first = *s_b;
while (++i >= 0 && first->index != push)
first = first->next;
if (i <= size / 2)
{
while (j++ < i)
rotate_b(s_b, 1);
}
else if (i > size / 2)
{
while (first != NULL && j++ >= 0)
first = first->next;
i = j;
while (j-- > 0)
r_rotate_b(s_b, 1);
}
push_a(s_a, s_b, 1);
}
void remplisage_a(t_stack **s_a, t_stack **s_b, int size)
{
int max;
max = max_int(*s_b);
while (max >= 0)
push_int(s_a, s_b, max--, size--);
}
void check_correct(t_stack **stk)
{
t_stack *temp;
temp = *stk;
while (temp->next != NULL)
{
if (temp->index > temp->next->index)
break ;
temp = temp->next;
}
}
void rank_stacks_util(t_stack **stk, t_stack **temp,
t_stack **rank, int size)
{
int i;
i = 0;
while (*temp != *rank && ++i > 0)
{
if (*temp == NULL)
*temp = *stk;
if ((*rank)->x == (*temp)->x && i != size)
ft_error(NULL);
if ((*rank)->x < (*temp)->x)
*temp = (*temp)->next;
else if ((*rank)->x > (*temp)->x && (*rank)->index++ >= 0)
*temp = (*temp)->next;
}
}
void rank_stacks(t_stack **stk, int size)
{
t_stack *rank;
t_stack *temp;
rank = *stk;
temp = rank->next;
while (rank != NULL)
{
rank_stacks_util(stk, &temp, &rank, size);
if (rank->next == NULL)
break ;
rank = rank->next;
temp = rank->next;
if (temp == NULL)
temp = *stk;
}
check_correct(stk);
}