diff --git a/builds/algo.o b/builds/algo.o index fa70d6e..b1b7158 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/print.o b/builds/print.o index 6b69c9a..4d75650 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/pushswap.o b/builds/pushswap.o index 0a5be1c..f394b4e 100644 Binary files a/builds/pushswap.o and b/builds/pushswap.o differ diff --git a/builds/swap.o b/builds/swap.o index ef5b28d..13f234d 100644 Binary files a/builds/swap.o and b/builds/swap.o differ diff --git a/includes/pushswap.h b/includes/pushswap.h index 6e35209..441bdec 100644 --- a/includes/pushswap.h +++ b/includes/pushswap.h @@ -22,55 +22,47 @@ t_stack *init_stack(int ac, char **av); ** stop.c */ void ps_usage(void); -void ps_error(int err); -void ps_stop(t_stack *stb, t_stack *sta, t_list *lst, int i); +void ps_error(int i); +void ps_stop(t_stack *a, t_stack *b, t_list *lst, int i); /* ** algo.c */ -t_list *sort_algo(t_stack *stack, int i); +t_list *sort_algo(t_stack *stack); /* ** print.c */ -void print_result(t_list *result); +void fill_solution(t_stack **a, t_stack **b, t_list **lst, char *c); void print_stack(t_stack *stack, char c); +void print_result(t_list *lst); /* ** swap */ -void sa(t_list *pack); -void sb(t_list *pack); -void ss(t_list *pack); +t_list *sa(t_stack **stack, t_list **lst); +t_list *sb(t_stack **stack, t_list **lst); +t_list *ss(t_stack **a, t_stack **b, t_list **lst); /* ** push */ -/* -int push(t_list **dst, t_list **src); -int pa(); -int pb(); -*/ +t_list *pa(t_stack **dst, t_stack **src, t_list **lst); +t_list *pb(t_stack **dst, t_stack **src, t_list **lst); /* ** rotate */ -/* -int rotate(t_list **list); -int ra(); -int rb(); -int rr(); -*/ +t_list *ra(t_stack **stack, t_list **lst); +t_list *rb(t_stack **stack, t_list **lst); +t_list *rr(t_stack **a, t_stack **b, t_list **lst); /* ** reverse rotate */ -/* -int reverse_rotate(t_list **list); -int rra(); -int rrb(); -int rrr(); -*/ +t_list *rra(t_stack **stack, t_list **lst); +t_list *rrb(t_stack **stack, t_list **lst); +t_list *rrr(t_stack **a, t_stack **b, t_list **lst); #endif diff --git a/pushswap b/pushswap index 10c091c..ae511b4 100755 Binary files a/pushswap and b/pushswap differ diff --git a/srcs/algo.c b/srcs/algo.c index a935aa5..387e70d 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -1,34 +1,20 @@ #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_stack *stack_a, t_stack *stack_b, t_list *solution, int flag, - void (*sp)(t_list *pack)) +void hugo_sort(t_stack **a, t_list **solution) { - 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_putchar('t'); - ft_printf("%s\n", (*solution)->content); - ft_putchar('u'); - print_stack(stack_a, 'a'); - print_stack(stack_b, 'b'); + sa(a, solution); + sa(a, solution); + sa(a, solution); } /* -** +** this function creates the stack b and the list solution +** then it calls the sorting algorithm +** then it frees everything that needs to be freed */ -t_list *sort_algo(t_stack *a, int f) +t_list *sort_algo(t_stack *a) { t_stack *b; t_list *solution; @@ -36,21 +22,7 @@ t_list *sort_algo(t_stack *a, int f) solution = NULL; if(!(b = ft_calloc(1, sizeof(t_stack)))) ps_stop(a, b, NULL, 2); - action(a, b, solution, f, sa); - action(a, b, solution, f, 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 + hugo_sort(&a, &solution); + ps_stop(NULL, b, NULL, -1); + return (solution); } diff --git a/srcs/print.c b/srcs/print.c index 5fa676c..1965d9c 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,13 +1,11 @@ #include "pushswap.h" -void print_result(t_list *result) +void fill_solution(t_stack **a, t_stack **b, t_list **solution, char *sp) { - while (result) - { - ft_printf("%s\n", result->content); - result = result->next; - } + (void)a; + (void)b; + ft_lstadd_back(solution, ft_lstnew(sp)); } void print_stack(t_stack *stack, char c) @@ -23,3 +21,11 @@ void print_stack(t_stack *stack, char c) ft_putchar('\n'); } +void print_result(t_list *result) +{ + while (result) + { + ft_printf("%s\n", result->content); + result = result->next; + } +} diff --git a/srcs/pushswap.c b/srcs/pushswap.c index eb16f9f..96a371b 100644 --- a/srcs/pushswap.c +++ b/srcs/pushswap.c @@ -44,8 +44,9 @@ int main(int ac, char **av) is_valid(ac, av); // check if usage and list are correct flag = check_flag(&ac, &av); //check for flag, like print stack = init_stack(ac, av); // create the list from av[] - result = sort_algo(stack, flag); -// print_result(result); + result = sort_algo(stack); + (void)flag; + print_result(result); ps_stop(stack, NULL, result, 0); return(0); } diff --git a/srcs/swap.c b/srcs/swap.c index 5cb01e3..838efc1 100644 --- a/srcs/swap.c +++ b/srcs/swap.c @@ -1,19 +1,18 @@ #include "pushswap.h" -void sa(t_list *pack) +t_list *sa(t_stack **stack, t_list **solution) { - t_stack *stack_a; - t_list **solution; - int tmp; + t_stack *a; + int tmp; - 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; - ft_lstadd_back(solution, ft_lstnew(ft_strdup("sa"))); + a = *stack; + if (!a->next) + return (NULL); + tmp = a->n; + a->n = a->next->n; + a->next->n = tmp; + fill_solution(stack, NULL, solution, ft_strdup("sa")); + return (NULL); }