This commit is contained in:
kinou-p 2022-01-10 16:04:12 +01:00
parent c582aec0ba
commit 0480f150b2
7 changed files with 77 additions and 8 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2021/11/13 13:06:47 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 \ SRCS = push_swap.c \
process.c\ process.c\
main.c\ main.c\
sorting.c\
indexing.c indexing.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CFLAGS = CFLAGS =

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 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; void *nbr;
int index; int index;
int swap;
struct s_list *next; struct s_list *next;
} t_list; } t_list;

12
main.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:43 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[]) int main(int argc, char *argv[])
{ {
push_swap(argc, 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); return (1);
} }

View File

@ -12,7 +12,7 @@
#include "push_swap.h" #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; t_list *swap;
@ -20,6 +20,13 @@ void ft_sa_sb(t_list **list, t_list **process, char type)
(*list)->next->next = *list; (*list)->next->next = *list;
*list = (*list)->next; *list = (*list)->next;
(*list)->next->next = swap; (*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) 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; (ft_lstlast(*list))->next = *list;
*list = (*list)->next; *list = (*list)->next;
swap->next = 0; 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) 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; swap = *list_b;
*list_b = (*list_b)->next; *list_b = (*list_b)->next;
ft_lstadd_front(list_a, swap); 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) 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; swap = *list_a;
*list_a = (*list_a)->next; *list_a = (*list_a)->next;
ft_lstadd_front(list_b, swap); 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) void ft_rra_rrb(t_list **list, t_list **process, char type)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:10:37 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); start = set_list(--nbrarg, list);
lst_indexing(start); lst_indexing(start);
printf_list(start); printf_list(start);
find_loop_index(start);
if (is_sorted(start)) if (is_sorted(start))
{ {
ft_lstclear(&start, &free); ft_lstclear(&start, &free);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:17 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 <unistd.h> # include <unistd.h>
# include "./libft/libft.h" # include "./libft/libft.h"
/*typedef struct process_list typedef struct process_list
{ {
void *action; void *action;
struct process_list *next; struct process_list *next;
} action_list;*/ } s_list;
void sort3(t_list *list); void sort3(t_list *list);
int push_swap(int nbrarg, char **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); void lst_indexing(t_list *list);
int is_double(int nbrarg, char **list); int is_double(int nbrarg, char **list);
int is_sorted(t_list *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);

39
s_list.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}