-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations_1.c
77 lines (70 loc) · 2.16 KB
/
operations_1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* operations_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvonsovs <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 13:33:07 by fvonsovs #+# #+# */
/* Updated: 2023/04/17 17:47:23 by fvonsovs ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// swap the first 2 elements at the top of stack
int sa_sb(t_stack **stack, int mode)
{
t_stack *temp;
if (!stack || !*stack || !(*stack)->next)
return (1);
temp = *stack;
*stack = (*stack)->next;
temp->next = (*stack)->next;
(*stack)->next = temp;
if (mode == 1)
ft_printf("sa\n");
else
ft_printf("sb\n");
return (0);
}
// sa and sb at the same time
int ss(t_stack **stack_a, t_stack **stack_b)
{
t_stack *temp;
if (!stack_a || !*stack_a || !(*stack_a)->next)
return (1);
temp = *stack_a;
*stack_a = (*stack_a)->next;
temp->next = (*stack_a)->next;
(*stack_a)->next = temp;
temp = *stack_b;
*stack_b = (*stack_b)->next;
temp->next = (*stack_b)->next;
(*stack_b)->next = temp;
ft_printf("ss\n");
return (0);
}
// mode 1 = first element at the top of b and put it at the top of a
// mode 0 = first element at the top of a and put it at the top of b.
int pa_pb(t_stack **stack_a, t_stack **stack_b, int mode)
{
t_stack *temp;
if (!stack_a || !*stack_a)
return (1);
if (mode == 1)
{
temp = *stack_b;
*stack_b = (*stack_b)->next;
temp->next = NULL;
stackadd_front(stack_a, temp);
ft_printf("pa\n");
}
else
{
temp = *stack_a;
*stack_a = (*stack_a)->next;
temp->next = NULL;
stackadd_front(stack_b, temp);
ft_printf("pb\n");
}
return (0);
}