diff --git a/process.c b/process.c index 82b057a..f37d2e9 100644 --- a/process.c +++ b/process.c @@ -12,7 +12,7 @@ #include "push_swap.h" -void ft_sa_sb(t_list **list, t_list **process, char type, int set) +t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set) { t_list *swap; @@ -22,14 +22,14 @@ void ft_sa_sb(t_list **list, t_list **process, char type, int set) (*list)->next->next = swap; if (set) { - if (type) - ft_lstadd_back(process, new_slist("sa")); - else - ft_lstadd_back(process, new_slist("sb")); + if (type = 'a') + s_lstadd_back(process, new_slist("sa")); + else if (type = 'b') + s_lstadd_back(process, new_slist("sb")); } } -void ft_ra_rb(t_list **list, t_list **process, char type) +t_list *ft_ra_rb(t_list **list, s_list **process, char type) { t_list *swap; @@ -37,33 +37,33 @@ void ft_ra_rb(t_list **list, t_list **process, char type) (ft_lstlast(*list))->next = *list; *list = (*list)->next; swap->next = 0; - if (type) - ft_lstadd_back(process, new_slist("ra")); - else - ft_lstadd_back(process, new_slist("rb")); + if (type = 'a') + s_lstadd_back(process, new_slist("ra")); + else if (type = 'b') + s_lstadd_back(process, new_slist("rb")); } -void ft_pa(t_list **list_a, t_list **list_b, t_list **process) +t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process) { t_list *swap; swap = *list_b; *list_b = (*list_b)->next; ft_lstadd_front(list_a, swap); - ft_lstadd_back(process, new_slist("pa")); + s_lstadd_back(process, new_slist("pa")); } -void ft_pb(t_list **list_a, t_list **list_b, t_list **process) +t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process) { t_list *swap; swap = *list_a; *list_a = (*list_a)->next; ft_lstadd_front(list_b, swap); - ft_lstadd_back(process, new_slist("pb")); + s_lstadd_back(process, new_slist("pb")); } -void ft_rra_rrb(t_list **list, t_list **process, char type) +t_list * ft_rra_rrb(t_list **list, s_list **process, char type) { t_list *swap; diff --git a/push_swap.c b/push_swap.c index 19b593d..47ed205 100644 --- a/push_swap.c +++ b/push_swap.c @@ -23,6 +23,7 @@ void printf_list(t_list *start) printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index); start = start->next; } + printf("----end_print----"); return; } diff --git a/push_swap.h b/push_swap.h index c68b71f..48b0d44 100644 --- a/push_swap.h +++ b/push_swap.h @@ -31,13 +31,22 @@ void lst_indexing(t_list *list); int is_double(int nbrarg, char **list); int is_sorted(t_list *list); void find_loop_index(t_list *list); -int find_loop(t_list *start, t_list *list); +int find_loop(t_list *start, t_list *list, int setswap); t_list *ft_lstnew(void *content); void s_lstclear(s_list **lst, void (*del)(void*)); -void swap_to_b(t_list list, t_list best,int loop); +void swap_to_b(t_list *list, t_list *best, int loop); +s_list *new_slist(void *content); +void s_lstadd_back(s_list **alst, s_list *new); +s_list *s_lstlast(s_list *lst); +t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set); +t_list *ft_ra_rb(t_list **list, s_list **process, char type); +t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process); +t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process); +void printf_list(t_list *start); + #endif \ No newline at end of file diff --git a/s_list.c b/s_list.c index d981db8..91d2f34 100644 --- a/s_list.c +++ b/s_list.c @@ -10,6 +10,8 @@ /* */ /* ************************************************************************** */ +#include "push_swap.h" + s_list *new_slist(void *content) { s_list *new; @@ -17,9 +19,8 @@ s_list *new_slist(void *content) new = (s_list*)malloc(sizeof(s_list)); if (!new) return (0); - new->nbr = content; + new->action = content; new->next = 0; - new->swap = 0; return (new); } @@ -37,3 +38,20 @@ void s_lstclear(s_list **lst, void (*del)(void*)) } lst = 0; } + +void s_lstadd_back(s_list **alst, s_list *new) +{ + if (*alst == 0) + *alst = new; + else + ft_lstlast(*alst)->next = new; +} + +s_list *s_lstlast(s_list *lst) +{ + if (!lst) + return (0); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/sorting.c b/sorting.c index 7a106d2..1450d33 100644 --- a/sorting.c +++ b/sorting.c @@ -12,6 +12,7 @@ #include "push_swap.h" + void find_loop_index(t_list *list) { t_list *start; @@ -33,6 +34,7 @@ void find_loop_index(t_list *list) list = list->next; } find_loop(start, save, 1); + swap_to_b(start, save, loop_size2); printf("loop_size = %d\n", loop_size2); } @@ -63,30 +65,37 @@ int find_loop(t_list *start, t_list *list, int setswap) return (loop_size); } -void swap_to_b(t_list list, t_list best,int loop) +void swap_to_b(t_list *list, t_list *best, int loop) { + s_list *action; int swap; - t_list start; + int lst_size; + t_list *b; - start = list; + b = 0; + action = 0; + lst_size = ft_lstsize(list); swap = 0; - while (list) + printf_list(list); + while (lst_size) { - ft_sa_sb(); - swap = find_loop(start, best); + ft_sa_sb(&list, &action, 'a', 0); + swap = find_loop(list, best, 0); if (swap > loop) { loop = swap; - add_sa(); + s_lstadd_back(&action, new_slist("sa")); } else - ft_sa_sb(); + ft_sa_sb(&list, &action, 'a', 0); if(list->swap) - ft_pa(); + ft_pb(&list, &b, &action); else if (!list->swap) - ft_ra_rb(); - list = list->next; + ft_ra_rb(&list, &action, 'a'); + lst_size--; + printf_list(list); } + printf("\nswap done\n"); }