-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.c
54 lines (44 loc) · 1.7 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abertran <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 16:13:16 by abertran #+# #+# */
/* Updated: 2023/03/08 20:35:05 by abertran ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* Swaps the top 2 elements of a stack.
Does nothing if there is only one or no elements. */
static void swap(t_stack **stack)
{
t_stack *tmp;
if (!*stack || (*stack)->next == NULL)
return ;
tmp = *stack;
*stack = (*stack)->next;
tmp->next = (*stack)->next;
(*stack)->next = tmp;
}
/* Swaps the top 2 elements of stack a. Prints "sa" to the standard output. */
void do_sa(t_stack **stack_a)
{
swap(stack_a);
ft_putstr("sa\n");
}
/* Swaps the top 2 elements of stack b. Prints "sb" to the standard output. */
void do_sb(t_stack **stack_b)
{
swap(stack_b);
ft_putstr("sb\n");
}
/* Swaps the top 2 elements of stack a and the top 2 elements
of stack b. Prints "ss" to the standard output. */
void do_ss(t_stack **stack_a, t_stack **stack_b)
{
swap(stack_a);
swap(stack_b);
ft_putstr("ss\n");
}