diff --git a/Makefile b/Makefile index 0d3701e..589f09f 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,13 @@ LIBS = $(_LIBS:lib%.a=%) SRCS = push_swap.c \ algo.c \ print.c \ + stop.c \ swap.c \ - stop.c + push.c \ + rotate.c \ + reverse_rotate.c \ + algo_bubble_sort.c + ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) diff --git a/README.md b/README.md index 98e110e..844c4c2 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,12 @@ source des comparaisons ci-dessous : [https://www.youtube.com/watch?v=xoR-1KwQh2 - `t_list *rrb(t_stack **stack, t_list **lst);` - `t_list *rrr(t_stack **a, t_stack **b, t_list **lst);` +## points particuliers : + +- les fonctions d'actions (puches, swapes, rotates) recoivent une ou les deux piles a et b, et la liste chainee *solution* qui doit accueillir les noms des actions successives +- mais pour pouvoir imprimer l'état des deux piles a et b a chaque etape il faudrait que chaques fonctions recoivent les deux piles, ce qui n'est pas le cas : sa() par exemple ne recoit pas la pile b +- donc j'ai stocké les deux piles dans les deux premiers maillons de la chaine solution, car toutes les fonctions d'actions la recoivent + ## ce programme push_swap.c s'organise de la facon suivante : 1. des fonctions pour faire fonctionner l'algorithme de tris (principalement parsing des donnees, et les actions utilisees par l'algorithme de tris : push, swap, rotate et reverse rotate) : diff --git a/builds/algo.o b/builds/algo.o index d5123c8..d5927b0 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..2a7786b 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 2459d08..327efb8 100644 Binary files a/builds/print.o and b/builds/print.o differ diff --git a/builds/push.o b/builds/push.o new file mode 100644 index 0000000..270ea32 Binary files /dev/null and b/builds/push.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o index 49fcc6d..62a47a1 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 new file mode 100644 index 0000000..5cb39d0 Binary files /dev/null and b/builds/reverse_rotate.o differ diff --git a/builds/rotate.o b/builds/rotate.o new file mode 100644 index 0000000..d05d39a Binary files /dev/null and b/builds/rotate.o differ diff --git a/builds/stop.o b/builds/stop.o index 1fb1ed2..145d304 100644 Binary files a/builds/stop.o and b/builds/stop.o differ diff --git a/builds/swap.o b/builds/swap.o index e64c942..d16f654 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 642b288..8bd120b 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -40,10 +40,9 @@ void hugo_sort(t_stack **a, t_stack **b, t_list **lst); /* ** print.c */ -void fill_solution(t_stack *a, t_stack *b, t_list **lst, char *c); +void fill_solution(t_list **lst, char *c); void print_result(t_list *lst, int i); - /* ** swap */ diff --git a/push_swap b/push_swap index 9c07b88..81f132f 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/algo.c b/srcs/algo.c index b998c92..ecafeea 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -5,7 +5,7 @@ void hugo_sort(t_stack **a, t_stack **b, t_list **solution) { (void)b; sa(a, solution); - sa(a, solution); - sa(a, solution); + pb(b, a, solution); + sb(a, solution); } diff --git a/srcs/algo_bubble_sort.c b/srcs/algo_bubble_sort.c index 01812fc..c485215 100644 --- a/srcs/algo_bubble_sort.c +++ b/srcs/algo_bubble_sort.c @@ -12,6 +12,7 @@ #include "push_swap.h" +/* static int search_smaller(t_stack *stack, int *smaller) { int i; @@ -56,3 +57,4 @@ 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 2464411..406f324 100644 --- a/srcs/print.c +++ b/srcs/print.c @@ -1,10 +1,28 @@ #include "push_swap.h" -void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) +/* +** 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 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 : +** name!a: nb nb nb nb nb!b: nb nb nb +** "!" are used to split, it will be turn into : +** name +** a: nb nb nb nb nb +** b: nb nb nb +*/ +void fill_solution(t_list **solution, char *sp) { + t_stack *a; + t_stack *b; char *stack; + a = (*solution)->content; + b = (*solution)->next->content; stack = ft_strdup(sp); stack = ft_strjoinfree(stack, ft_strdup("!a:")); while(a) @@ -17,7 +35,7 @@ void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) while(b) { stack = ft_strjoinfree(stack, ft_strdup(" ")); - stack = ft_strjoinfree(stack, ft_itoa(a->n)); + stack = ft_strjoinfree(stack, ft_itoa(b->n)); b = b->next; } ft_lstadd_back(solution, ft_lstnew(stack)); @@ -26,7 +44,8 @@ void fill_solution(t_stack *a, t_stack *b, t_list **solution, char *sp) void print_result(t_list *result, int flag) { char **part; - + + result = result->next->next; if (!flag) result = result->next; while (result) diff --git a/srcs/push.c b/srcs/push.c new file mode 100644 index 0000000..8b67a8a --- /dev/null +++ b/srcs/push.c @@ -0,0 +1,37 @@ + +#include "push_swap.h" + +t_list *pa(t_stack **a, t_stack **b, t_list **solution) +{ + t_stack *tmp; + + if (!(tmp = ft_calloc(1, sizeof(t_stack)))) + ps_stop(*a, *b, *solution, 2); + tmp->n = (*b)->n; + tmp->next = NULL; + (*a)->next = tmp; + fill_solution(solution, ft_strdup("pa")); + return (NULL); +} + +t_list *pb(t_stack **dst, t_stack **src, t_list **solution) +{ + t_stack *a; + t_stack *b; + t_stack *tmp; + + a = *src; + b = *dst; + + if (!(b = ft_calloc(1, sizeof(t_stack)))) + ps_stop(a, b, *solution, 2); + b->n = a->n; + b->next = NULL; + + tmp = a->next; + a->n = a->next->n; + a->next = a->next->next; + free(tmp); + fill_solution(solution, ft_strdup("pa")); + return (NULL); +} diff --git a/srcs/push_swap.c b/srcs/push_swap.c index f46eae9..485093e 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -49,7 +49,10 @@ 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 initial values of stack a (in case of flag -p) +** 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 */ @@ -58,11 +61,13 @@ t_list *launch_algo(t_stack *a) t_stack *b; t_list *solution; - if(!(b = ft_calloc(1, sizeof(t_stack)))) - ps_stop(a, b, NULL, 2); + b = NULL; solution = NULL; - fill_solution(a, NULL, &solution, ft_strdup("start")); + ft_lstadd_back(&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); ps_stop(NULL, b, NULL, -1); return (solution); } diff --git a/srcs/reverse_rotate.c b/srcs/reverse_rotate.c new file mode 100644 index 0000000..d8ff985 --- /dev/null +++ b/srcs/reverse_rotate.c @@ -0,0 +1,6 @@ + +#include "push_swap.h" + +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); diff --git a/srcs/rotate.c b/srcs/rotate.c new file mode 100644 index 0000000..a6212b5 --- /dev/null +++ b/srcs/rotate.c @@ -0,0 +1,7 @@ + +#include "push_swap.h" + +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); + diff --git a/srcs/swap.c b/srcs/swap.c index e03320f..77242f8 100644 --- a/srcs/swap.c +++ b/srcs/swap.c @@ -12,7 +12,18 @@ t_list *sa(t_stack **stack, t_list **solution) tmp = a->n; a->n = a->next->n; a->next->n = tmp; - fill_solution(*stack, NULL, solution, ft_strdup("sa")); + fill_solution(solution, ft_strdup("sa")); return (NULL); } +t_list *sb(t_stack **b, t_list **solution) +{ + return (sa(b, solution)); +} + +t_list *ss(t_stack **a, t_stack **b, t_list **solution) +{ + sa(a, solution); + sa(b, solution); + return (NULL); +}