-
Notifications
You must be signed in to change notification settings - Fork 0
/
rev_rotate.c
64 lines (58 loc) · 1.69 KB
/
rev_rotate.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rev_rotate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stigkas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 14:15:24 by stigkas #+# #+# */
/* Updated: 2024/02/08 17:07:59 by stigkas ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/push_swap.h"
static int rev_rotate(t_stack **stack)
{
t_stack *head;
t_stack *last;
if (!(*stack) || ft_stack_size(*stack) <= 1)
return (-1);
head = *stack;
last = ft_stacklast(head);
while (head)
{
if (head->next->next == NULL)
{
head->next = NULL;
break ;
}
head = head->next;
}
last->next = *stack;
*stack = last;
return (0);
}
void rra(t_stack **stack_a)
{
if (rev_rotate(stack_a) == -1)
ft_error("Error\n");
else
write(1, &"rra\n", 4);
}
void rrb(t_stack **stack_b)
{
if (rev_rotate(stack_b) == -1)
ft_error("Error\n");
else
write(1, &"rrb\n", 4);
}
void rrr(t_stack **stack_a, t_stack **stack_b)
{
if (ft_stack_size(*stack_a) <= 1 || ft_stack_size(*stack_b) <= 1)
ft_error("Error\n");
else
{
rev_rotate(stack_a);
rev_rotate(stack_b);
write(1, &"rrr\n", 4);
}
}