-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves_push_swap.c
82 lines (71 loc) · 2 KB
/
moves_push_swap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* moves_push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpalacio <lpalacio@student.42madrid> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/23 19:49:59 by lpalacio #+# #+# */
/* Updated: 2024/03/21 22:30:53 by lpalacio ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int swap(t_list **stack, char *str)
{
t_list *aux;
if (!(*stack)->next)
return (0);
aux = *stack;
*stack = aux->next;
aux->next = (*stack)->next;
(*stack)->next = aux;
ft_putstr_fd(str, 1);
return (1);
}
int push(t_list **lst_src, t_list **lst_dst, char *str)
{
t_list *node;
if (!*lst_src)
return (0);
node = *lst_src;
*lst_src = node->next;
ft_lstadd_front(lst_dst, node);
ft_putstr_fd(str, 1);
return (1);
}
int rotate(t_list **stack, char *str)
{
t_list *node;
if (!*stack || !(*stack)->next)
return (0);
node = *stack;
*stack = node->next;
node->next = NULL;
ft_lstadd_back(stack, node);
ft_putstr_fd(str, 1);
return (1);
}
int reverse(t_list **stack, char *str)
{
t_list *node;
t_list *aux;
if (!*stack || !(*stack)->next)
return (0);
node = ft_lstlast(*stack);
aux = *stack;
while (aux->next != node)
aux = aux->next;
aux->next = NULL;
ft_lstadd_front(stack, node);
ft_putstr_fd(str, 1);
return (1);
}
void push_all_a(t_list **lst_src, t_list **lst_dst)
{
int stack_size;
stack_size = ft_lstsize(*lst_src);
while (stack_size--)
{
push(lst_src, lst_dst, "pa\n");
}
}