sort3 done

This commit is contained in:
whaffman 2024-11-07 15:15:20 +01:00
parent 7797fe91c4
commit bf0aa15453
8 changed files with 20 additions and 24 deletions

View File

@ -18,10 +18,10 @@ LIBFT_INC_PATH = libft/inc
LIBFT = libft/libft.a LIBFT = libft/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/util:src/moves VPATH = src:src/util:src/moves:src/sort
SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \ SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \
swap.c swap.c sort3.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -63,7 +63,7 @@ fclean: clean
re: fclean all re: fclean all
run: all run: all
@$(eval ARG = $(shell shuf -i 0-100 -n 5)) @$(eval ARG = $(shell shuf -i 0-100 -n 3))
./$(NAME) $(ARG) ./$(NAME) $(ARG)
debug: CFLAGS += -DDEBUG -g debug: CFLAGS += -DDEBUG -g

View File

@ -26,6 +26,8 @@ t_list *ft_lstat(t_list *list, int n);
void print_stack(const char *name, t_list *stack); void print_stack(const char *name, t_list *stack);
void print_stacks(t_list *stack_a, t_list *stack_b); void print_stacks(t_list *stack_a, t_list *stack_b);
int sort3(t_state *state);
void push(t_list **stack_1, t_list **stack_2); void push(t_list **stack_1, t_list **stack_2);
void rotate(t_list **stack); void rotate(t_list **stack);
void reverse_rotate(t_list **stack); void reverse_rotate(t_list **stack);

View File

@ -28,5 +28,4 @@ int print_pointer(va_list args);
int parse_conversion(const char **format, va_list args); int parse_conversion(const char **format, va_list args);
int parse_placeholder(const char **format, va_list args); int parse_placeholder(const char **format, va_list args);
#endif #endif

View File

@ -20,14 +20,6 @@ typedef struct s_list
struct s_list *next; struct s_list *next;
} t_list; } t_list;
typedef struct s_clist
{
void *content;
struct s_clist *next;
struct s_clist *prev;
} t_clist;
int ft_isalpha(int c); int ft_isalpha(int c);
int ft_isdigit(int c); int ft_isdigit(int c);
int ft_isalnum(int c); int ft_isalnum(int c);

View File

@ -36,6 +36,8 @@ void swap(t_list **stack)
{ {
int *temp; int *temp;
if (!*stack)
return ;
temp = (*stack)->next->content; temp = (*stack)->next->content;
(*stack)->next->content = (*stack)->content; (*stack)->next->content = (*stack)->content;
(*stack)->content = temp; (*stack)->content = temp;

View File

@ -70,6 +70,7 @@ int main(int argc, char *argv[])
return (1); return (1);
} }
} }
sort3(state);
print_state(state); print_state(state);
return (0); return (0);
} }

View File

@ -20,29 +20,29 @@
// cab ra // cab ra
// cba sa rra // cba sa rra
int sort3(t_list **stack) int sort3(t_state *state)
{ {
const int a = *((int *)(*stack)->content); const int a = *((int *) state->a->content);
const int b = *((int *)(*stack)->next->content); const int b = *((int *) state->a->next->content);
const int c = *((int *)(*stack)->next->next->content); const int c = *((int *) state->a->next->next->content);
if (ft_lstsize(*stack) != 3) if (ft_lstsize(state->a) != 3)
return (0); return (0);
if (a < b && b < c) if (a < b && b < c)
return (1); return (1);
else if (a < b && b > c) else if (a < b && b > c)
{ {
rra(); rra(state);
if (a > b) if (a < c)
sa(); sa(state);
} }
else if (a > c && b < c) else if (a > c && b < c)
ra(); ra(state);
else else
{ {
sa(); sa(state);
if (b > c) if (b > c)
rra(); rra(state);
} }
return (1); return (1);
} }