-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations1.c
68 lines (60 loc) · 1.67 KB
/
operations1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vsosevic <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 12:44:43 by vsosevic #+# #+# */
/* Updated: 2017/03/09 12:45:37 by vsosevic ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void shift_down(t_stack *stk)
{
int i;
i = stk->nbrs_l;
while (i > 0)
{
stk->nbrs[i] = stk->nbrs[i - 1];
i--;
}
stk->nbrs_l++;
}
void shift_up(t_stack *stk)
{
int i;
i = 0;
while (i < stk->nbrs_l - 1)
{
stk->nbrs[i] = stk->nbrs[i + 1];
i++;
}
stk->nbrs_l--;
}
void swap_sx(t_stack *stk, char **cmds)
{
if (stk->nbrs_l > 1)
{
ft_swap_int(&stk->nbrs[0], &stk->nbrs[1]);
if (stk->name == 'a')
(*cmds) = ft_strjoin_m(*cmds, "sa\n");
else
(*cmds) = ft_strjoin_m(*cmds, "sb\n");
}
}
void swap_ss(t_stack *a, t_stack *b, char **cmds)
{
swap_sx(a, cmds);
swap_sx(b, cmds);
}
void push_a(t_stack *a, t_stack *b, char **cmds)
{
if (b->nbrs_l > 0)
{
shift_down(a);
a->nbrs[0] = b->nbrs[0];
shift_up(b);
(*cmds) = ft_strjoin_m(*cmds, "pa\n");
}
}