Files
42_INT_06_pushwap/srcs/push_swap.c
2021-06-27 13:30:04 +02:00

75 lines
1.4 KiB
C

#include "push_swap.h"
void is_valid(int ac, char **av)
{
if (ac < 2)
ps_stop(NULL, NULL, 1);
(void)av;
// check more error
}
int check_flag(int *ac, char ***av)
{
if (ft_strcmp((*av)[1], "-p") != 0)
return (0);
(*av)++;;
(*ac)--;
return (1);
}
t_stack *init_stack(int ac, char **av)
{
t_stack *start;
t_stack *tmp;
tmp = NULL;
while (--ac)
{
if (!(start = ft_calloc(1, sizeof(t_stack))))
ps_stop(NULL, NULL, 2);
start->n = ft_atoi(av[ac]);
start->next = tmp;
tmp = start;
}
return (start);
}
/*
** the chained list "solution" is created with specials first two elements :
** they are pointers to stack a and b, so it can be accessed by fill_solution()
**
** because when an action like sa() is called it desn't get stack_b as a parameter
** so when it calls fill_solution(), this function needs an access to stack_b in
** case of flag -p, to fill the state of the stacks
*/
t_list *launch_algo(t_stack **a, t_stack **b, int flag)
{
t_list *solution;
solution = ft_lstnew(a);
ft_lstadd_back(&solution, ft_lstnew(b));
if (flag)
fill_solution(solution, "start");
hugo_sort(a, b, solution);
//bubble_sort(&a, &b, solution);
return (solution);
}
int main(int ac, char **av)
{
t_stack *a;
t_stack *b;
t_list *solution;
int flag;
is_valid(ac, av);
flag = check_flag(&ac, &av);
a = init_stack(ac, av);
b = NULL;
solution = launch_algo(&a, &b, flag);
print_result(solution, flag);
ps_stop(NULL, solution, 0);
return(0);
}