-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.c
106 lines (98 loc) · 2.51 KB
/
operations.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmatjuhi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 13:50:46 by kmatjuhi #+# #+# */
/* Updated: 2024/01/20 14:42:44 by kmatjuhi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void swap_second(t_stack **lst)
{
t_stack *first;
t_stack *second;
if ((*lst)->next->next == *lst)
{
*lst = (*lst)->next;
return ;
}
first = *lst;
second = (*lst)->next;
first->next = second->next;
second->next->prev = first;
second->prev = first->prev;
first->prev = second;
second->next = first;
second->prev->next = second;
*lst = second;
}
void swap(t_stack **a_lst, t_stack **b_lst, char c)
{
if (c == 'a' || c == 's')
{
if (!(*a_lst) || (*a_lst)->next == *a_lst)
return ;
swap_second(a_lst);
}
if (c == 'b' || c == 's')
{
if (!(*b_lst) || (*b_lst)->next == *b_lst)
return ;
swap_second(b_lst);
}
ft_printf("s%c\n", c);
}
void push(t_stack **src, t_stack **dest, char c)
{
t_stack *temp;
if (*src == NULL)
return ;
temp = *src;
if (*src == (*src)->next)
*src = NULL;
else
{
*src = temp->next;
temp->prev->next = *src;
(*src)->prev = temp->prev;
temp->next = temp;
temp->prev = temp;
}
add_front(&(*dest), temp);
ft_printf("p%c\n", c);
}
void rotate(t_stack **a_lst, t_stack **b_lst, char c)
{
if (c == 'a' || c == 'r')
{
if (!(*a_lst) || (*a_lst)->next == *a_lst)
return ;
*a_lst = (*a_lst)->next;
}
if (c == 'b' || c == 'r')
{
if (!(*b_lst) || (*b_lst)->next == *b_lst)
return ;
*b_lst = (*b_lst)->next;
}
ft_printf("r%c\n", c);
}
void reverse_rotate(t_stack **a_lst, t_stack **b_lst, char c)
{
if (c == 'a' || c == 'r')
{
if (!(*a_lst) || (*a_lst)->next == *a_lst)
return ;
*a_lst = (*a_lst)->prev;
}
if (c == 'b' || c == 'r')
{
if (!(*b_lst) || (*b_lst)->next == *b_lst)
return ;
*b_lst = (*b_lst)->prev;
}
ft_printf("rr%c\n", c);
}