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
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 \
swap.c
swap.c sort3.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
@ -63,7 +63,7 @@ fclean: clean
re: fclean all
run: all
@$(eval ARG = $(shell shuf -i 0-100 -n 5))
@$(eval ARG = $(shell shuf -i 0-100 -n 3))
./$(NAME) $(ARG)
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_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 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_placeholder(const char **format, va_list args);
#endif

View File

@ -20,14 +20,6 @@ typedef struct s_list
struct s_list *next;
} 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_isdigit(int c);
int ft_isalnum(int c);

View File

@ -22,7 +22,7 @@ int print_pointer(va_list args)
size_t len;
char *str;
int wrote;
n = va_arg(args, unsigned long);
if (!n)
return (write(1, "(nil)", 5));

View File

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

View File

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

View File

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