-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_rotations.c
61 lines (55 loc) · 1.73 KB
/
reverse_rotations.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reverse_rotations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rjobert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/30 18:59:47 by rjobert #+# #+# */
/* Updated: 2023/07/15 16:25:09 by rjobert ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* how to reverse rotate (last node become first)
1- we go to the almost last node
2 - we save in temp the last node
3 - we make the almost last node the lst (point null)
4 - we make temp point to head and head = temp */
void reverse_rotate(t_list **stack)
{
t_list *head;
t_list *temp;
t_list *save;
if (!*stack || !(*stack)->next)
return ;
head = *stack;
save = *stack;
while (head->next->next)
head = head->next;
temp = head->next;
temp->next = save;
save->prev = temp;
head->next = NULL;
temp->prev = NULL;
head = temp;
*stack = head;
}
void rra(t_list **a, int n)
{
reverse_rotate(a);
if (n != 0)
ft_putstr("rra\n");
}
void rrb(t_list **b, int n)
{
reverse_rotate(b);
if (n != 0)
ft_putstr("rrb\n");
}
void rrr(t_list **a, t_list **b, int n)
{
reverse_rotate(a);
reverse_rotate(b);
if (n != 0)
ft_putstr("rrr\n");
}