push et swap fonctionnent
This commit is contained in:
BIN
builds/algo.o
BIN
builds/algo.o
Binary file not shown.
BIN
builds/print.o
BIN
builds/print.o
Binary file not shown.
BIN
builds/push.o
BIN
builds/push.o
Binary file not shown.
Binary file not shown.
BIN
builds/swap.o
BIN
builds/swap.o
Binary file not shown.
@@ -35,12 +35,12 @@ void ps_stop(t_stack *a, t_stack *b, t_list *lst, int i);
|
|||||||
/*
|
/*
|
||||||
** algo.c
|
** algo.c
|
||||||
*/
|
*/
|
||||||
void hugo_sort(t_stack **a, t_stack **b, t_list **lst);
|
void hugo_sort(t_stack **a, t_stack **b, t_list *lst);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** print.c
|
** print.c
|
||||||
*/
|
*/
|
||||||
void fill_solution(t_list **lst, char *c);
|
void fill_solution(t_list *lst, char *c);
|
||||||
void print_result(t_list *lst, int i);
|
void print_result(t_list *lst, int i);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
14
srcs/algo.c
14
srcs/algo.c
@@ -1,11 +1,17 @@
|
|||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
void hugo_sort(t_stack **a, t_stack **b, t_list **solution)
|
void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||||
{
|
{
|
||||||
(void)b;
|
(void)b;
|
||||||
sa(a, solution);
|
|
||||||
pb(b, a, solution);
|
sa(a, &solution);
|
||||||
sb(a, solution);
|
pb(b, a, &solution);
|
||||||
|
pb(b, a, &solution);
|
||||||
|
pb(b, a, &solution);
|
||||||
|
pa(a, b, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
sb(b, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
srcs/print.c
12
srcs/print.c
@@ -15,14 +15,18 @@
|
|||||||
** a: nb nb nb nb nb
|
** a: nb nb nb nb nb
|
||||||
** b: nb nb nb
|
** b: nb nb nb
|
||||||
*/
|
*/
|
||||||
void fill_solution(t_list **solution, char *sp)
|
void fill_solution(t_list *solution, char *sp)
|
||||||
{
|
{
|
||||||
|
t_stack **tmp1;
|
||||||
|
t_stack **tmp2;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
t_stack *b;
|
t_stack *b;
|
||||||
char *stack;
|
char *stack;
|
||||||
|
|
||||||
a = (*solution)->content;
|
tmp1 = solution->content;
|
||||||
b = (*solution)->next->content;
|
tmp2 = solution->next->content;
|
||||||
|
a = *tmp1;
|
||||||
|
b = *tmp2;
|
||||||
stack = ft_strdup(sp);
|
stack = ft_strdup(sp);
|
||||||
stack = ft_strjoinfree(stack, ft_strdup("!a:"));
|
stack = ft_strjoinfree(stack, ft_strdup("!a:"));
|
||||||
while(a)
|
while(a)
|
||||||
@@ -38,7 +42,7 @@ void fill_solution(t_list **solution, char *sp)
|
|||||||
stack = ft_strjoinfree(stack, ft_itoa(b->n));
|
stack = ft_strjoinfree(stack, ft_itoa(b->n));
|
||||||
b = b->next;
|
b = b->next;
|
||||||
}
|
}
|
||||||
ft_lstadd_back(solution, ft_lstnew(stack));
|
ft_lstadd_back(&solution, ft_lstnew(stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_result(t_list *result, int flag)
|
void print_result(t_list *result, int flag)
|
||||||
|
|||||||
43
srcs/push.c
43
srcs/push.c
@@ -1,37 +1,28 @@
|
|||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void push(t_stack **dst, t_stack **src)
|
||||||
|
{
|
||||||
|
t_stack *tmp1;
|
||||||
|
t_stack *tmp2;
|
||||||
|
|
||||||
|
tmp1 = *src;
|
||||||
|
tmp2 = *dst;
|
||||||
|
*src = (*src)->next;
|
||||||
|
tmp1->next = tmp2;
|
||||||
|
*dst = tmp1;
|
||||||
|
}
|
||||||
|
|
||||||
t_list *pa(t_stack **a, t_stack **b, t_list **solution)
|
t_list *pa(t_stack **a, t_stack **b, t_list **solution)
|
||||||
{
|
{
|
||||||
t_stack *tmp;
|
push(a, b);
|
||||||
|
fill_solution(*solution, ft_strdup("pa"));
|
||||||
if (!(tmp = ft_calloc(1, sizeof(t_stack))))
|
|
||||||
ps_stop(*a, *b, *solution, 2);
|
|
||||||
tmp->n = (*b)->n;
|
|
||||||
tmp->next = NULL;
|
|
||||||
(*a)->next = tmp;
|
|
||||||
fill_solution(solution, ft_strdup("pa"));
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *pb(t_stack **dst, t_stack **src, t_list **solution)
|
t_list *pb(t_stack **b, t_stack **a, t_list **solution)
|
||||||
{
|
{
|
||||||
t_stack *a;
|
push(b, a);
|
||||||
t_stack *b;
|
fill_solution(*solution, ft_strdup("pb"));
|
||||||
t_stack *tmp;
|
|
||||||
|
|
||||||
a = *src;
|
|
||||||
b = *dst;
|
|
||||||
|
|
||||||
if (!(b = ft_calloc(1, sizeof(t_stack))))
|
|
||||||
ps_stop(a, b, *solution, 2);
|
|
||||||
b->n = a->n;
|
|
||||||
b->next = NULL;
|
|
||||||
|
|
||||||
tmp = a->next;
|
|
||||||
a->n = a->next->n;
|
|
||||||
a->next = a->next->next;
|
|
||||||
free(tmp);
|
|
||||||
fill_solution(solution, ft_strdup("pa"));
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,12 +63,11 @@ t_list *launch_algo(t_stack *a)
|
|||||||
|
|
||||||
b = NULL;
|
b = NULL;
|
||||||
solution = NULL;
|
solution = NULL;
|
||||||
ft_lstadd_back(&solution, ft_lstnew(a));
|
ft_lstadd_back(&solution, ft_lstnew(&a));
|
||||||
ft_lstadd_back(&solution, ft_lstnew(b));
|
ft_lstadd_back(&solution, ft_lstnew(&b));
|
||||||
fill_solution(&solution, ft_strdup("start"));
|
fill_solution(solution, ft_strdup("start"));
|
||||||
hugo_sort(&a, &b, &solution);
|
hugo_sort(&a, &b, solution);
|
||||||
//bubble_sort(&a, &b, solution);
|
//bubble_sort(&a, &b, solution);
|
||||||
ps_stop(NULL, b, NULL, -1);
|
|
||||||
return (solution);
|
return (solution);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +90,5 @@ int main(int ac, char **av)
|
|||||||
stack = init_stack(ac, av);
|
stack = init_stack(ac, av);
|
||||||
result = launch_algo(stack);
|
result = launch_algo(stack);
|
||||||
print_result(result, flag);
|
print_result(result, flag);
|
||||||
ps_stop(stack, NULL, result, 0);
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|||||||
33
srcs/swap.c
33
srcs/swap.c
@@ -1,29 +1,36 @@
|
|||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
t_list *sa(t_stack **stack, t_list **solution)
|
void swap(t_stack **stack)
|
||||||
{
|
{
|
||||||
t_stack *a;
|
t_stack *tmp;
|
||||||
int tmp;
|
|
||||||
|
|
||||||
a = *stack;
|
tmp = *stack;
|
||||||
if (!a->next)
|
if (!(tmp->next))
|
||||||
return (NULL);
|
return ;
|
||||||
tmp = a->n;
|
*stack = (*stack)->next;
|
||||||
a->n = a->next->n;
|
tmp->next = (*stack)->next;
|
||||||
a->next->n = tmp;
|
(*stack)->next = tmp;
|
||||||
fill_solution(solution, ft_strdup("sa"));
|
}
|
||||||
|
|
||||||
|
t_list *sa(t_stack **a, t_list **solution)
|
||||||
|
{
|
||||||
|
swap(a);
|
||||||
|
fill_solution(*solution, ft_strdup("sa"));
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *sb(t_stack **b, t_list **solution)
|
t_list *sb(t_stack **b, t_list **solution)
|
||||||
{
|
{
|
||||||
return (sa(b, solution));
|
swap(b);
|
||||||
|
fill_solution(*solution, ft_strdup("sb"));
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *ss(t_stack **a, t_stack **b, t_list **solution)
|
t_list *ss(t_stack **a, t_stack **b, t_list **solution)
|
||||||
{
|
{
|
||||||
sa(a, solution);
|
swap(a);
|
||||||
sa(b, solution);
|
swap(b);
|
||||||
|
fill_solution(*solution, ft_strdup("sb"));
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user