Files
42_INT_06_pushwap/srcs/algo.c

80 lines
2.1 KiB
C

#include "pushswap.h"
/*
** this function is only used to print the stack and the action
** then it launch the action (swap, push, rotate)
*/
void action(t_list *pack, void (*sp)(t_list *pack))
{
t_stack *stack_a;
t_stack *stack_b;
t_list **solution;
int flag;
stack_a = pack->content;
stack_b = pack->next->content;
solution = pack->next->next->content;
flag = *(int *)(pack->next->next->next->content); // this is a really ugly line :p it's because lst->content is a void*, but we need an int
if (!flag) // if flag == 0, don't print anything
return ;
if (!*solution) // to print only at first call
{
print_stack(stack_a, 'a');
print_stack(stack_b, 'b');
}
sp(pack); // the action to execute
ft_printf("%s\n", (*solution)->content);
print_stack(stack_a, 'a');
print_stack(stack_b, 'b');
}
t_list *do_pack(t_stack *stack_a, int flag)
{
t_stack *stack_b;
t_list *pack;
if(!(stack_b = ft_calloc(1, sizeof(t_stack))))
ps_stop(stack_a, stack_b, NULL, 2);
pack = ft_lstnew(stack_a);
ft_lstadd_back(&pack, ft_lstnew(stack_b));
ft_lstadd_back(&pack, ft_lstnew(NULL));
ft_lstadd_back(&pack, ft_lstnew(&flag));
return (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, int flag)
{
t_list *pack;
pack = do_pack(stack, flag);
action(pack, sa);
action(pack, sa);
// 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);
if (flag)
ft_putchar('\n');
ps_stop(NULL, pack->next->content, NULL, -1); // pack->next->content is stack_b
return (pack->next->next->content); // pack->next->next->content is t_list *solution
}