diff --git a/Makefile b/Makefile index bf9c1f2..7399e93 100644 --- a/Makefile +++ b/Makefile @@ -73,7 +73,7 @@ leaks: CFLAGS += -fsanitize=address leaks: clean $(NAME) valgrind: clean $(NAME) - valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) -p 4 67 9 76 98 7 1 .PHONY: all clean fclean re gcc diff --git a/builds/algo.o b/builds/algo.o index 15f25ba..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 2a7786b..9eebf0c 100644 Binary files a/builds/algo_bubble_sort.o and b/builds/algo_bubble_sort.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/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 b73699f..6d42092 100644 Binary files a/builds/rotate.o and b/builds/rotate.o differ diff --git a/builds/swap.o b/builds/swap.o index 7673fb8..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 619c1a1..e1443f8 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -23,11 +23,12 @@ 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); +t_list *launch_algo(t_stack **a, t_stack **b, int i); /* ** stop.c */ +void print_test(t_list *lst, char *s); void ps_usage(void); void ps_error(int i); void ps_stop(t_stack *stack, t_list *lst, int i); @@ -40,15 +41,14 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *lst); /* ** print.c */ -void print_test(t_list *lst, char *s); void fill_solution(t_list *lst, char *s); 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/srcs/algo.c b/srcs/algo.c index efd08bc..f0411b6 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -1,12 +1,7 @@ - #include "push_swap.h" void hugo_sort(t_stack **a, t_stack **b, t_list *solution) { - (void)b; - - pb(b, a, &solution); - /* sa(a, &solution); pb(b, a, &solution); pb(b, a, &solution); @@ -23,6 +18,4 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *solution) ra(a, &solution); rb(b, &solution); rr(a, b, &solution); - */ } - diff --git a/srcs/algo_bubble_sort.c b/srcs/algo_bubble_sort.c index c485215..01812fc 100644 --- a/srcs/algo_bubble_sort.c +++ b/srcs/algo_bubble_sort.c @@ -12,7 +12,6 @@ #include "push_swap.h" -/* static int search_smaller(t_stack *stack, int *smaller) { int i; @@ -57,4 +56,3 @@ int bubble_sort(t_stack **a, t_stack **b, t_list *solution) pa(a, b, &solution); return (1); } -*/ diff --git a/srcs/print.c b/srcs/print.c index 5753cbe..1144421 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,42 +1,34 @@ - #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 is called by actions like sa() +** it fills 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 no flag -p */ void fill_solution(t_list *solution, char *sp) { - t_stack **tmp1; - t_stack **tmp2; t_stack *a; t_stack *b; char *stack; - tmp1 = solution->content; - tmp2 = solution->next->content; - a = *tmp1; - b = *tmp2; - stack = ft_strdup(sp); - stack = ft_strjoinfree(stack, ft_strdup("!a:")); - while(a != NULL) + a = *(t_stack **)(solution->content); + b = *(t_stack **)(solution->next->content); + stack = ft_strjoinfree(ft_strdup(sp), ft_strdup("!a:")); + while (a != NULL) { stack = ft_strjoinfree(stack, ft_strdup(" ")); stack = ft_strjoinfree(stack, ft_itoa(a->n)); a = a->next; } stack = ft_strjoinfree(stack, ft_strdup("!b:")); - while(b) + while (b != NULL) { stack = ft_strjoinfree(stack, ft_strdup(" ")); stack = ft_strjoinfree(stack, ft_itoa(b->n)); @@ -50,8 +42,6 @@ void print_result(t_list *result, int flag) char **part; result = result->next->next; - if (!flag) - result = result->next; while (result) { part = ft_split(result->content, '!'); @@ -62,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..c6a2a1b 100644 --- a/srcs/push.c +++ b/srcs/push.c @@ -1,4 +1,3 @@ - #include "push_swap.h" void push(t_stack **dst, t_stack **src) @@ -15,16 +14,16 @@ void push(t_stack **dst, t_stack **src) *dst = tmp1; } -t_list *pa(t_stack **a, t_stack **b, t_list **solution) +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) +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 a06832c..447c0b4 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -1,35 +1,22 @@ - #include "push_swap.h" -/* -** this function check errors in the arguments -** like, is there arguments, and are they a valid list without equals values -** if there are no arguments it calls the stop function with value 1 to print usage -*/ +// check more error void is_valid(int ac, char **av) { if (ac < 2) ps_stop(NULL, NULL, 1); (void)av; - // check more error } -/* -** this function check if there are flags (currently only one flag) : -** -p to print the evolution of the list while the sorting is done -*/ -int check_flag(int *ac, char ***av) +int check_flag(int *ac, char ***av) { if (ft_strcmp((*av)[1], "-p") != 0) return (0); - (*av)++;; + (*av)++; (*ac)--; return (1); } -/* -** create the stack for list a, as a linked list, and fill it with the values given in arguments argv -*/ t_stack *init_stack(int ac, char **av) { t_stack *start; @@ -38,7 +25,8 @@ t_stack *init_stack(int ac, char **av) tmp = NULL; while (--ac) { - if (!(start = ft_calloc(1, sizeof(t_stack)))) + start = ft_calloc(1, sizeof(t_stack)); + if (!start) ps_stop(NULL, NULL, 2); start->n = ft_atoi(av[ac]); start->next = tmp; @@ -47,48 +35,34 @@ t_stack *init_stack(int ac, char **av) return (start); } -/* -** 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 -*/ -t_list *launch_algo(t_stack *a) +// 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() +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)); - fill_solution(solution, ft_strdup("start")); - hugo_sort(&a, &b, solution); - //bubble_sort(&a, &b, solution); + solution = ft_lstnew(a); + ft_lstadd_back(&solution, ft_lstnew(b)); + if (flag) + fill_solution(solution, "start"); + hugo_sort(a, b, solution); return (solution); } +// bubble_sort(&a, &b, 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) +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); - print_result(result, flag); - ps_stop(NULL, result, 0); - return(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..57a61bb 100644 --- a/srcs/reverse_rotate.c +++ b/srcs/reverse_rotate.c @@ -1,7 +1,6 @@ - #include "push_swap.h" -void reverse_rotate(t_stack **stack) +void reverse_rotate(t_stack **stack) { t_stack *tmp; t_stack *before; @@ -19,24 +18,24 @@ void reverse_rotate(t_stack **stack) before->next = NULL; } -t_list *rra(t_stack **a, t_list **lst) +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) +t_list *rrb(t_stack **b, t_list **lst) { reverse_rotate(b); - fill_solution(*lst, ft_strdup("rrb")); + fill_solution(*lst, "rrb"); return (NULL); } -t_list *rrr(t_stack **a, t_stack **b, t_list **lst) +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/rotate.c b/srcs/rotate.c index 68b9c32..9988136 100644 --- a/srcs/rotate.c +++ b/srcs/rotate.c @@ -1,7 +1,6 @@ - #include "push_swap.h" -void rotate(t_stack **stack) +void rotate(t_stack **stack) { t_stack *tmp; t_stack *first; @@ -17,25 +16,24 @@ void rotate(t_stack **stack) first->next = NULL; } -t_list *ra(t_stack **a, t_list **lst) +t_list *ra(t_stack **a, t_list **lst) { rotate(a); - fill_solution(*lst, ft_strdup("ra")); + fill_solution(*lst, "ra"); return (NULL); } -t_list *rb(t_stack **b, t_list **lst) +t_list *rb(t_stack **b, t_list **lst) { rotate(b); - fill_solution(*lst, ft_strdup("rb")); + fill_solution(*lst, "rb"); return (NULL); } -t_list *rr(t_stack **a, t_stack **b, t_list **lst) +t_list *rr(t_stack **a, t_stack **b, t_list **lst) { rotate(a); rotate(b); - fill_solution(*lst, ft_strdup("rr")); + fill_solution(*lst, "rr"); return (NULL); } - diff --git a/srcs/stop.c b/srcs/stop.c index 6b9105e..e8dafe2 100644 --- a/srcs/stop.c +++ b/srcs/stop.c @@ -1,4 +1,3 @@ - #include "push_swap.h" void ps_usage(void) @@ -12,16 +11,14 @@ void ps_error(int err) if (err == 1) ps_usage(); if (err == 2) - ft_printf("error\n"); + ft_printf("error\n"); exit(0); } 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 +28,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; } diff --git a/srcs/swap.c b/srcs/swap.c index 2bac144..9fd4048 100644 --- a/srcs/swap.c +++ b/srcs/swap.c @@ -1,4 +1,3 @@ - #include "push_swap.h" void swap(t_stack **stack) @@ -13,24 +12,24 @@ void swap(t_stack **stack) (*stack)->next = tmp; } -t_list *sa(t_stack **a, t_list **solution) +t_list *sa(t_stack **a, t_list **solution) { swap(a); - fill_solution(*solution, ft_strdup("sa")); + fill_solution(*solution, "sa"); return (NULL); } -t_list *sb(t_stack **b, t_list **solution) +t_list *sb(t_stack **b, t_list **solution) { swap(b); - fill_solution(*solution, ft_strdup("sb")); + fill_solution(*solution, "sb"); return (NULL); } -t_list *ss(t_stack **a, t_stack **b, t_list **solution) +t_list *ss(t_stack **a, t_stack **b, t_list **solution) { swap(a); swap(b); - fill_solution(*solution, ft_strdup("sb")); + fill_solution(*solution, "sb"); return (NULL); }