-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutions.c
96 lines (85 loc) · 2.17 KB
/
executions.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:15:11 by dvan-der #+# #+# */
/* Updated: 2021/12/15 08:15:13 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void swap(t_stack *one, t_stack *two)
{
int x;
char *y;
x = one->d_nbr;
one->d_nbr = two->d_nbr;
two->d_nbr = x;
y = one->b_nbr;
one->b_nbr = two->b_nbr;
two->b_nbr = y;
}
void execute_s(t_stack **stack, char c)
{
t_stack *tmp1;
tmp1 = (*stack)->next;
swap((*stack), tmp1);
if (c == 'a')
write(1, "sa\n", 3);
else if (c == 'b')
write(1, "sb\n", 3);
}
void execute_r(t_stack **stack, char c)
{
t_stack *tmp1;
tmp1 = *stack;
*stack = tmp1->next;
tmp1->next = NULL;
stack_add_back(stack, tmp1);
if (c == 'a')
write(1, "ra\n", 3);
else if (c == 'b')
write(1, "rb\n", 3);
}
void execute_rr(t_stack **stack, char c)
{
t_stack *tmp1;
t_stack *tmp2;
tmp1 = *stack;
while (tmp1->next != NULL)
{
tmp2 = tmp1;
tmp1 = tmp1->next;
}
stack_add_front(stack, tmp1);
tmp2->next = NULL;
if (c == 'a')
write(1, "rra\n", 4);
else if (c == 'b')
write(1, "rrb\n", 4);
}
void execute_p(t_stack **a, t_stack **b, char c)
{
t_stack *new;
new = NULL;
if (c == 'b')
{
new = new_node((*a)->d_nbr, (*a)->b_nbr);
stack_add_front(b, new);
new = *a;
*a = new->next;
free(new);
write(1, "pb\n", 3);
}
if (c == 'a')
{
new = new_node((*b)->d_nbr, (*b)->b_nbr);
stack_add_front(a, new);
new = *b;
*b = new->next;
free(new);
write(1, "pa\n", 3);
}
}