diff --git a/Makefile b/Makefile index 4530356..d5da6ca 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2021/11/13 13:06:47 by apommier #+# #+# # -# Updated: 2022/01/06 23:59:23 by apommier ### ########.fr # +# Updated: 2022/01/08 18:27:15 by apommier ### ########.fr # # # # **************************************************************************** # @@ -14,6 +14,7 @@ NAME = push_swap SRCS = push_swap.c \ process.c\ main.c\ + sorting.c\ indexing.c OBJS = ${SRCS:.c=.o} CFLAGS = diff --git a/libft/libft.h b/libft/libft.h index c3e045a..805d6d5 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ -/* Updated: 2022/01/07 17:55:25 by apommier ### ########.fr */ +/* Updated: 2022/01/09 18:49:47 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ typedef struct s_list { void *nbr; int index; + int swap; struct s_list *next; } t_list; diff --git a/main.c b/main.c index 37795a5..250f21a 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:43 by apommier #+# #+# */ -/* Updated: 2021/11/25 23:57:06 by apommier ### ########.fr */ +/* Updated: 2022/01/08 22:12:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,14 @@ int main(int argc, char *argv[]) { + if (argc == 1) + { + ft_putstr_fd("Error\n", 2); + return (1); + } + if (argc == 2) + return (1); push_swap(argc, argv); + printf("------end------\n"); return (1); } \ No newline at end of file diff --git a/process.c b/process.c index f5679b2..82b057a 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) +void ft_sa_sb(t_list **list, t_list **process, char type, int set) { t_list *swap; @@ -20,6 +20,13 @@ void ft_sa_sb(t_list **list, t_list **process, char type) (*list)->next->next = *list; *list = (*list)->next; (*list)->next->next = swap; + if (set) + { + if (type) + ft_lstadd_back(process, new_slist("sa")); + else + ft_lstadd_back(process, new_slist("sb")); + } } void ft_ra_rb(t_list **list, t_list **process, char type) @@ -30,6 +37,10 @@ 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")); } void ft_pa(t_list **list_a, t_list **list_b, t_list **process) @@ -39,6 +50,7 @@ void ft_pa(t_list **list_a, t_list **list_b, t_list **process) swap = *list_b; *list_b = (*list_b)->next; ft_lstadd_front(list_a, swap); + ft_lstadd_back(process, new_slist("pa")); } void ft_pb(t_list **list_a, t_list **list_b, t_list **process) @@ -48,6 +60,7 @@ void ft_pb(t_list **list_a, t_list **list_b, t_list **process) swap = *list_a; *list_a = (*list_a)->next; ft_lstadd_front(list_b, swap); + ft_lstadd_back(process, new_slist("pb")); } void ft_rra_rrb(t_list **list, t_list **process, char type) diff --git a/push_swap.c b/push_swap.c index c7000d6..19b593d 100644 --- a/push_swap.c +++ b/push_swap.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ -/* Updated: 2022/01/07 18:57:42 by apommier ### ########.fr */ +/* Updated: 2022/01/08 18:25:27 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -91,6 +91,7 @@ int push_swap(int nbrarg, char **list) start = set_list(--nbrarg, list); lst_indexing(start); printf_list(start); + find_loop_index(start); if (is_sorted(start)) { ft_lstclear(&start, &free); diff --git a/push_swap.h b/push_swap.h index 3285426..c68b71f 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ -/* Updated: 2022/01/06 04:38:07 by apommier ### ########.fr */ +/* Updated: 2022/01/09 18:51:38 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,11 @@ # include # include "./libft/libft.h" -/*typedef struct process_list +typedef struct process_list { void *action; struct process_list *next; -} action_list;*/ +} s_list; void sort3(t_list *list); int push_swap(int nbrarg, char **list); @@ -30,6 +30,12 @@ t_list *set_list(int nbrarg, char **list); 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); +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); + diff --git a/s_list.c b/s_list.c new file mode 100644 index 0000000..d981db8 --- /dev/null +++ b/s_list.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/09 18:46:50 by apommier #+# #+# */ +/* Updated: 2022/01/09 18:46:50 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +s_list *new_slist(void *content) +{ + s_list *new; + + new = (s_list*)malloc(sizeof(s_list)); + if (!new) + return (0); + new->nbr = content; + new->next = 0; + new->swap = 0; + return (new); +} + +void s_lstclear(s_list **lst, void (*del)(void*)) +{ + s_list *chr; + + chr = *lst; + while (*lst) + { + chr = (*lst)->next; + del((*lst)->action); + free(*lst); + *lst = chr; + } + lst = 0; +}