-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap_utils2.c
92 lines (84 loc) · 2.28 KB
/
push_swap_utils2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 15:53:16 by minsuki2 #+# #+# */
/* Updated: 2022/07/22 21:14:43 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *repeat_next(t_stack *top, int n)
{
while (top && n--)
top = top->next;
return (top);
}
t_stack *repeat_priv(t_stack *top, int n)
{
while (top && n--)
top = top->priv;
return (top);
}
char sort_push_method(t_cursor *head)
{
int i;
int j;
int check;
i = 0;
j = 0;
while (repeat_next(head->cur_a, i)->tmp_idx > 1)
i++;
while (repeat_priv(head->cur_a, j)->tmp_idx > 1)
j++;
if (i <= j)
while (i--)
rx(head, head->cur_a);
else if (i > j)
while (j--)
rrx(head, head->cur_a);
px(head, head->cur_a, head->cur_b);
check = case_check(head);
px(head, head->cur_b, head->cur_a);
sort_swap(head, head->cur_a);
return (check);
}
int sort_swap(t_cursor *head, t_stack *top)
{
if (!top || !top->next)
return (ERROR);
if (top->tmp_idx - top->next->tmp_idx == 1)
{
sx(head, top);
return (SUCCESS);
}
if (((top->tmp_idx == 0 && top->next->tmp_idx == head->cnt_a - 1)
&& next_check(top->next->next) == SUCCESS))
{
sx(head, top);
return (SUCCESS);
}
return (ERROR);
}
int where_idx_n(t_cursor *head, int n, char spot)
{
int find_idx;
t_stack *tmp;
tmp = head->cur_a;
if (spot == '2')
tmp = head->cur_b;
if (!tmp)
return (ERROR);
find_idx = tmp->idx;
while (tmp && n--)
{
if (spot == '0' && tmp->idx < find_idx)
find_idx = tmp->idx;
else if (spot == '2' && tmp->idx > find_idx)
find_idx = tmp->idx;
tmp = tmp->next;
}
return (find_idx);
}