push et swap fonctionnent

This commit is contained in:
hugogogo
2021-06-15 14:27:55 +02:00
parent aeb5b57725
commit dc99227f94
12 changed files with 61 additions and 55 deletions

View File

@@ -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);
}

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}