diff --git a/builds/algo.o b/builds/algo.o index d5927b0..80a5d0c 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/print.o b/builds/print.o index 327efb8..08ed267 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/push.o b/builds/push.o index 270ea32..f3d6f90 100644 Binary files a/builds/push.o and b/builds/push.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o index 62a47a1..c1666a5 100644 Binary files a/builds/push_swap.o and b/builds/push_swap.o differ diff --git a/builds/swap.o b/builds/swap.o index d16f654..7673fb8 100644 Binary files a/builds/swap.o and b/builds/swap.o differ diff --git a/includes/push_swap.h b/includes/push_swap.h index 8bd120b..ba6547f 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -35,12 +35,12 @@ void ps_stop(t_stack *a, t_stack *b, t_list *lst, int i); /* ** 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 */ -void fill_solution(t_list **lst, char *c); +void fill_solution(t_list *lst, char *c); void print_result(t_list *lst, int i); /* diff --git a/push_swap b/push_swap index 81f132f..400f861 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/algo.c b/srcs/algo.c index ecafeea..255783c 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -1,11 +1,17 @@ #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; - sa(a, solution); - pb(b, a, solution); - sb(a, solution); + + sa(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); } diff --git a/srcs/print.c b/srcs/print.c index 406f324..18f2721 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -15,14 +15,18 @@ ** a: nb nb 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 *b; char *stack; - a = (*solution)->content; - b = (*solution)->next->content; + tmp1 = solution->content; + tmp2 = solution->next->content; + a = *tmp1; + b = *tmp2; stack = ft_strdup(sp); stack = ft_strjoinfree(stack, ft_strdup("!a:")); while(a) @@ -38,7 +42,7 @@ void fill_solution(t_list **solution, char *sp) stack = ft_strjoinfree(stack, ft_itoa(b->n)); 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) diff --git a/srcs/push.c b/srcs/push.c index 8b67a8a..ad60a77 100644 --- a/srcs/push.c +++ b/srcs/push.c @@ -1,37 +1,28 @@ #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_stack *tmp; - - 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")); + push(a, b); + fill_solution(*solution, ft_strdup("pa")); 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; - t_stack *b; - 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")); + push(b, a); + fill_solution(*solution, ft_strdup("pb")); return (NULL); } diff --git a/srcs/push_swap.c b/srcs/push_swap.c index 485093e..73f06dd 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -63,12 +63,11 @@ t_list *launch_algo(t_stack *a) b = NULL; solution = NULL; - ft_lstadd_back(&solution, ft_lstnew(a)); - ft_lstadd_back(&solution, ft_lstnew(b)); - fill_solution(&solution, ft_strdup("start")); - hugo_sort(&a, &b, &solution); + ft_lstadd_back(&solution, ft_lstnew(&a)); + ft_lstadd_back(&solution, ft_lstnew(&b)); + fill_solution(solution, ft_strdup("start")); + hugo_sort(&a, &b, solution); //bubble_sort(&a, &b, solution); - ps_stop(NULL, b, NULL, -1); return (solution); } @@ -91,6 +90,5 @@ int main(int ac, char **av) stack = init_stack(ac, av); result = launch_algo(stack); print_result(result, flag); - ps_stop(stack, NULL, result, 0); return(0); } diff --git a/srcs/swap.c b/srcs/swap.c index 77242f8..2bac144 100644 --- a/srcs/swap.c +++ b/srcs/swap.c @@ -1,29 +1,36 @@ #include "push_swap.h" -t_list *sa(t_stack **stack, t_list **solution) +void swap(t_stack **stack) { - t_stack *a; - int tmp; + t_stack *tmp; - a = *stack; - if (!a->next) - return (NULL); - tmp = a->n; - a->n = a->next->n; - a->next->n = tmp; - fill_solution(solution, ft_strdup("sa")); + tmp = *stack; + if (!(tmp->next)) + return ; + *stack = (*stack)->next; + tmp->next = (*stack)->next; + (*stack)->next = tmp; +} + +t_list *sa(t_stack **a, t_list **solution) +{ + swap(a); + fill_solution(*solution, ft_strdup("sa")); return (NULL); } 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) { - sa(a, solution); - sa(b, solution); + swap(a); + swap(b); + fill_solution(*solution, ft_strdup("sb")); return (NULL); }