mise en place de l'algrithme

This commit is contained in:
hugogogo
2021-06-12 02:20:56 +02:00
parent 1993a9a85d
commit ca0bf0cb61
7 changed files with 77 additions and 9 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
pushswap

Binary file not shown.

View File

@@ -1,12 +1,78 @@
#include "pushswap.h"
t_list *sort_algo(t_stack *stack)
void sa(t_list *pack)
{
(void)stack;
t_list *lst;
t_stack *stack_a;
t_list **solution;
int tmp;
lst = ft_lstnew(ft_strdup("solution"));
return (lst);
stack_a = pack->content;
solution = pack->next->next->content;
if (!stack_a->next)
return ;
tmp = stack_a->n;
stack_a->n = stack_a->next->n;
stack_a->next->n = tmp;
(*solution)->content = ft_strdup("sa");
ft_lstadd_back(solution, ft_lstnew(NULL));
}
/*
** this function
*/
void action(t_list *pack, void (*sp)(t_list *pack))
{
t_stack *stack_a;
t_stack *stack_b;
t_list **solution;
stack_a = pack->content;
stack_b = pack->next->content;
solution = pack->next->next->content;
print_stack(stack_a);
print_stack(stack_b);
ft_printf("%s\n", (*solution)->content);
sp(pack);
}
/*
** this function create the structures "solution" and "stack_b"
** and put their together with "stack_a" in "pack" to facilitate
** the calls of functions. so pack contains :
** - stack_a
** - stack_b
** - solution
** then it calls the algorithm
*/
t_list *sort_algo(t_stack *stack_a)
{
t_stack *stack_b;
t_list *solution;
t_list *pack;
solution = ft_lstnew(NULL);
if(!(stack_b = ft_calloc(1, sizeof(t_stack))))
ps_stop(stack_a, stack_b, solution, 2);
pack = ft_lstnew(stack_a);
ft_lstadd_back(&pack, ft_lstnew(stack_b));
ft_lstadd_back(&pack, ft_lstnew(&solution));
action(pack, sa);
print_stack(stack_a);
print_stack(stack_b);
// sa(stack_a, stack_b, &solution);
// sb(stack_a, stack_b, &solution);
// ss(stack_a, stack_b, &solution);
// pa(stack_a, stack_b, &solution);
// pb(stack_a, stack_b, &solution);
// ra(stack_a, stack_b, &solution);
// rb(stack_a, stack_b, &solution);
// rr(stack_a, stack_b, &solution);
// rra(stack_a, stack_b, &solution);
// rrb(stack_a, stack_b, &solution);
// rrr(stack_a, stack_b, &solution);
return (solution);
}

View File

@@ -33,9 +33,8 @@ int main(int ac, char **av)
is_valid(ac, av); // check if usage and list are correct
stack = init_stack(ac, av); // create the list from av[]
print_stack(stack);
result = sort_algo(stack);
ft_printf("%s\n", result->content);
ft_printf("\n%s\n", result->content);
ps_stop(stack, NULL, result, 0);
return(0);
}

View File

@@ -23,6 +23,9 @@ void ps_stop(t_stack *stack_a, t_stack *stack_b, t_list *solution, int err)
ft_lstclear((t_list **)&stack_b, NULL);
if (solution)
ft_lstclear(&solution, free);
ps_error(err);
if (err < 0)
return ;
else if (err > 0)
ps_error(err);
exit(0);
}