-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_swap.c
68 lines (62 loc) · 1.91 KB
/
p_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* p_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dfurneau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 21:08:34 by dfurneau #+# #+# */
/* Updated: 2021/12/10 03:54:23 by dfurneau ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
/**
* Common Swap from both A and B
* Swap A or B (sa)(sb)
* Swap first 2 elements at the top of stack A or B
* Do nothing if there is only one or no elements
**/
static void swap_ab(t_stack *ab)
{
int tmp;
if (ab->top != -1)
{
tmp = ab->stack[ab->top];
ab->stack[ab->top] = ab->stack[ab->top - 1];
ab->stack[ab->top - 1] = tmp;
}
}
/**
* Swap A Function (sa)
* * Swap first 2 elements at the top of stack A
* Do nothing if there is only one or no elements
**/
void sa(t_stack *a)
{
swap_ab(a);
if (!CHECKER)
ft_printf("s%c\n", a->name);
}
/**
* Swap B Function (sb)
* Swap first 2 elements at the top of stack B
* Do nothing if there is only one or no elements
**/
void sb(t_stack *b)
{
swap_ab(b);
if (!CHECKER)
ft_printf("s%c\n", b->name);
}
/**
* Swap A and B (ss) Equal to (sa)(sb)
* Swap first 2 elements at the top of stack A and B
* Do nothing if there is only one or no elements
**/
void sss(t_stack *a, t_stack *b)
{
swap_ab(a);
swap_ab(b);
if (!CHECKER)
ft_printf("ss\n");
}