-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_commands.c
98 lines (87 loc) · 2.2 KB
/
push_commands.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lkramer <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/31 13:50:25 by lkramer #+# #+# */
/* Updated: 2025/02/10 14:48:43 by lkramer ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* first element becomes the last element.*/
void rotate_a(t_stack_node **a)
{
t_stack_node *first;
t_stack_node *last;
if (!(*a) || !(*a)->next)
return ;
first = *a;
last = *a;
while (last->next)
last = last->next;
*a = (*a)->next;
last->next = first;
first->next = NULL;
ft_printf("ra\n");
}
/* last element becomes the first.
*/
void reverse_rotate_a(t_stack_node **a)
{
t_stack_node *second_last;
t_stack_node *last;
if (!(*a) || !(*a)->next)
return ;
second_last = NULL;
last = *a;
while (last->next)
{
second_last = last;
last = last->next;
}
second_last->next = NULL;
last->next = *a;
*a = last;
ft_printf("rra\n");
}
/* Swap first two nodes in stack a */
void swap_a(t_stack_node **a)
{
int temp;
if (!(*a) || !(*a)->next)
return ;
temp = (*a)->nbr;
(*a)->nbr = (*a)->next->nbr;
(*a)->next->nbr = temp;
ft_printf("sa\n");
}
/* Pushes first node from stack b to top of
stack a */
void push_a(t_stack_node **a, t_stack_node **b)
{
t_stack_node *temp;
if (*b)
{
temp = *b;
*b = (*b)->next;
temp->next = *a;
*a = temp;
}
ft_printf("pa\n");
}
/* Pushes first node from stack a to top
of stack b */
void push_b(t_stack_node **a, t_stack_node **b)
{
t_stack_node *temp;
if (*a)
{
temp = *a;
*a = (*a)->next;
temp->next = *b;
*b = temp;
}
ft_printf("pb\n");
}