diff --git a/builds/algo.o b/builds/algo.o index 6f077cf..ef8b73d 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/algo_bubble_sort.o b/builds/algo_bubble_sort.o index a898837..9eebf0c 100644 Binary files a/builds/algo_bubble_sort.o and b/builds/algo_bubble_sort.o differ diff --git a/builds/print.o b/builds/print.o index ab8e164..c3caa16 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/push.o b/builds/push.o index 0e77e67..36bb4f9 100644 Binary files a/builds/push.o and b/builds/push.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o index 75073ba..d6bf2bf 100644 Binary files a/builds/push_swap.o and b/builds/push_swap.o differ diff --git a/builds/reverse_rotate.o b/builds/reverse_rotate.o index b3f6f9e..9f60562 100644 Binary files a/builds/reverse_rotate.o and b/builds/reverse_rotate.o differ diff --git a/builds/rotate.o b/builds/rotate.o index a534d1b..6d42092 100644 Binary files a/builds/rotate.o and b/builds/rotate.o differ diff --git a/builds/stop.o b/builds/stop.o index 22bffca..545288c 100644 Binary files a/builds/stop.o and b/builds/stop.o differ diff --git a/builds/swap.o b/builds/swap.o index cdfa0da..4e20baa 100644 Binary files a/builds/swap.o and b/builds/swap.o differ diff --git a/includes/push_swap.h b/includes/push_swap.h index 9fcd482..e1443f8 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -23,7 +23,7 @@ int bubble_sort(t_stack **a, t_stack **b, t_list *solution); int check_flag(int *ac, char ***av); void is_valid(int ac, char **av); t_stack *init_stack(int ac, char **av); -t_list *launch_algo(t_stack *stack, int i); +t_list *launch_algo(t_stack **a, t_stack **b, int i); /* ** stop.c @@ -47,8 +47,8 @@ void print_result(t_list *lst, int i); /* ** swap */ -t_list *sa(t_stack **stack, t_list **lst); -t_list *sb(t_stack **stack, t_list **lst); +t_list *sa(t_stack **a, t_list **lst); +t_list *sb(t_stack **b, t_list **lst); t_list *ss(t_stack **a, t_stack **b, t_list **lst); /* diff --git a/push_swap b/push_swap index 14c4c8c..497754f 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/algo.c b/srcs/algo.c index b25f482..b0e7a14 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -40,11 +40,9 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *solution) sa(a, &solution); rra(a, &solution); rrb(b, &solution); - /* rrr(a, b, &solution); ra(a, &solution); rb(b, &solution); rr(a, b, &solution); - */ } diff --git a/srcs/print.c b/srcs/print.c index 4f30948..05d64c5 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -2,18 +2,15 @@ #include "push_swap.h" /* -** this function is called at each action (pushes, swapes, rotates) -** pointers to the stacks a and b are stored into two first element of list solution (because both lists a and b are not available from every functions, like sa() doesn't receive b in argument) -** the function access the list after the two first elements to fill a new element with three things : -** - the name of the action (sa, sp, rra, etc) -** - the stack a -** - the stack b -** like this : +** this function fill a new element on the chained list like this : ** name!a: nb nb nb nb nb!b: nb nb nb -** "!" are used to split, it will be turn into : +** +** it will later be printed like this ("!" is the char to split) : ** name ** a: nb nb nb nb nb ** b: nb nb nb +** +** or only "name" if not flag -p */ void fill_solution(t_list *solution, char *sp) { @@ -55,6 +52,9 @@ void print_result(t_list *result, int flag) ft_printf(" %s\n", part[2]); } result = result->next; + free(part[0]); + free(part[1]); + free(part[2]); free(part); } } diff --git a/srcs/push.c b/srcs/push.c index e6461cb..704b78d 100644 --- a/srcs/push.c +++ b/srcs/push.c @@ -18,13 +18,13 @@ void push(t_stack **dst, t_stack **src) t_list *pa(t_stack **a, t_stack **b, t_list **solution) { push(a, b); - fill_solution(*solution, ft_strdup("pa")); + fill_solution(*solution, "pa"); return (NULL); } t_list *pb(t_stack **b, t_stack **a, t_list **solution) { push(b, a); - fill_solution(*solution, ft_strdup("pb")); + fill_solution(*solution, "pb"); return (NULL); } diff --git a/srcs/push_swap.c b/srcs/push_swap.c index bed0926..3e104cd 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -36,48 +36,39 @@ t_stack *init_stack(int ac, char **av) } /* -** this function creates the stack b and the list solution -** then it init the list solution with : -** - first element : pointer to stack a -** - secnd element : pointer to stack b -** - third element : initial values of stack a (in case of flag -p) -** then it calls the sorting algorithm -** then it calls ps_stop() to free everything that needs to be freed +** 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, int flag) +t_list *launch_algo(t_stack **a, t_stack **b, int flag) { - t_stack *b; t_list *solution; - b = NULL; - solution = ft_lstnew(&a); - ft_lstadd_back(&solution, ft_lstnew(&b)); + solution = ft_lstnew(a); + ft_lstadd_back(&solution, ft_lstnew(b)); if (flag) fill_solution(solution, "start"); - hugo_sort(&a, &b, solution); + hugo_sort(a, b, solution); //bubble_sort(&a, &b, solution); return (solution); } -/* -** this programme will sort a list -** this function first check the validity of the arguments -** then it checks for flags (currently only -p) -** then it parse the list into a linked list -** then it calls the sorting algorithm (it actually calls a launch function that will execute some actions and calls the sorting algorithm) -** and finally it frees everything and leaves -*/ int main(int ac, char **av) { - t_stack *stack; - t_list *result; + t_stack *a; + t_stack *b; + t_list *solution; int flag; is_valid(ac, av); flag = check_flag(&ac, &av); - stack = init_stack(ac, av); - result = launch_algo(stack, flag); - print_result(result, flag); - ps_stop(NULL, result, 0); + a = init_stack(ac, av); + b = NULL; + solution = launch_algo(&a, &b, flag); + print_result(solution, flag); + ps_stop(NULL, solution, 0); return(0); } diff --git a/srcs/reverse_rotate.c b/srcs/reverse_rotate.c index 60d4fcd..6d1c73a 100644 --- a/srcs/reverse_rotate.c +++ b/srcs/reverse_rotate.c @@ -22,14 +22,14 @@ void reverse_rotate(t_stack **stack) t_list *rra(t_stack **a, t_list **lst) { reverse_rotate(a); - fill_solution(*lst, ft_strdup("rra")); + fill_solution(*lst, "rra"); return (NULL); } t_list *rrb(t_stack **b, t_list **lst) { reverse_rotate(b); - fill_solution(*lst, ft_strdup("rrb")); + fill_solution(*lst, "rrb"); return (NULL); } @@ -37,6 +37,6 @@ t_list *rrr(t_stack **a, t_stack **b, t_list **lst) { reverse_rotate(a); reverse_rotate(b); - fill_solution(*lst, ft_strdup("rrr")); + fill_solution(*lst, "rrr"); return (NULL); } diff --git a/srcs/stop.c b/srcs/stop.c index a38fd0b..cdde4e8 100644 --- a/srcs/stop.c +++ b/srcs/stop.c @@ -18,10 +18,8 @@ void ps_error(int err) void ps_stop(t_stack *stack, t_list *lst, int err) { - /* t_stack **a; t_stack **b; - t_list *solution; if (stack) ft_lstclear((t_list **)&stack, NULL); @@ -31,48 +29,12 @@ void ps_stop(t_stack *stack, t_list *lst, int err) b = lst->next->content; ft_lstclear((t_list **)a, NULL); ft_lstclear((t_list **)b, NULL); + lst->content = NULL; + lst->next->content = NULL; ft_lstclear(&lst, free); } - (void)solution; - */ - - /* - t_list *tmp; - t_stack **aa; - t_stack **bb; - t_stack *a; - t_stack *b; - - aa = lst->content; - a = *aa; - bb = lst->next->content; - b = *bb; -// ft_putnbrendl(a->n); -// ft_putnbrendl(b->n); - - tmp = lst; - lst = lst->next; - free(tmp); - - tmp = lst; - lst = lst->next; - free(tmp); - - while (lst) - { - tmp = lst->next; - free(lst->content); - free(lst); - lst = tmp; - } - (void)a; - (void)b; - */ - if (err < 0) return ; else if (err > 0) ps_error(err); - (void)stack; - (void)lst; }