-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap_command_bonus.c
111 lines (103 loc) · 2.74 KB
/
push_swap_command_bonus.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
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_command_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/12 23:12:59 by minsuki2 #+# #+# */
/* Updated: 2022/07/22 21:54:43 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap_bonus.h"
char *ft_strchr_null(const char *s, int c)
{
size_t i;
i = 0;
while (*(s + i))
{
if (*(s + i) == (char)c)
return ((char *)s + i);
i++;
}
return ((char *)s + i);
}
int sx(t_stack *top)
{
int backup_int;
char backup_char;
if (!top || !top->next)
return (SUCCESS);
backup_int = top->next->idx;
top->next->idx = top->idx;
top->idx = backup_int;
backup_int = top->next->num;
top->next->num = top->num;
top->num = backup_int;
backup_char = top->next->spot;
top->next->spot = top->spot;
top->spot = backup_char;
return (SUCCESS);
}
int rx(t_cursor *head, t_stack *top)
{
if (!top || !top->next)
{
if (top)
top->spot += (top->spot % 2 == 0);
return (SUCCESS);
}
top->priv->next = top;
if (top->spot <= '1' )
head->cur_a = top->next;
else if (top->spot > '1')
head->cur_b = top->next;
top->spot += (top->spot % 2 == 0);
top->next = NULL;
return (SUCCESS);
}
int rrx(t_cursor *head, t_stack *top)
{
if (!top || !top->next)
{
if (top)
top->spot -= (top->spot % 2 != 0);
return (SUCCESS);
}
top->priv->next = top;
if (top->priv->spot <= '1')
head->cur_a = top->priv;
else if (top->priv->spot > '1')
head->cur_b = top->priv;
top->priv->spot -= (top->priv->spot % 2 != 0);
top->priv->priv->next = NULL;
return (SUCCESS);
}
int px(t_cursor *head, t_stack *_throw, t_stack *_catch)
{
if (!_throw)
return (SUCCESS);
if (_throw->next)
_throw->next->priv = _throw->priv;
_throw->priv = _throw;
if (_catch)
{
_throw->priv = _catch->priv;
_catch->priv = _throw;
}
head->cur_a = _throw->next;
head->cur_b = _throw;
if ((_throw->spot > '1') && ++head->cnt_a && head->cnt_b--)
{
head->cur_b = _throw->next;
head->cur_a = _throw;
}
_throw->next = _catch;
if ((_throw->spot <= '1') && head->cnt_a-- && ++head->cnt_b)
{
_throw->spot = '2';
return (SUCCESS);
}
_throw->spot = '0';
return (SUCCESS);
}