New insertion algorithm WIP

This commit is contained in:
David Gailleton
2025-12-11 18:42:33 +01:00
committed by David Gailleton
parent 1463f3e14a
commit f5da9fac1f
7 changed files with 197 additions and 8 deletions

View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */
/* Updated: 2025/12/09 13:49:13 by dgaillet ### ########lyon.fr */
/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */

View File

@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/11 17:46:00 by dgaillet #+# #+# */
/* Updated: 2025/12/11 17:46:22 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_lowest(t_stack *stack, t_stack *node, int len)
{
int i;
i = 0;
while (i < len)
{
if (node->value > stack->value)
return (0);
stack = stack->next;
i++;
}
return (1);
}
int is_highest(t_stack *stack, t_stack *node, int len)
{
int i;
i = 0;
while (i < len)
{
if (node->value < stack->value)
return (0);
stack = stack->next;
i++;
}
return (1);
}

View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iterate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/11 17:49:56 by dgaillet #+# #+# */
/* Updated: 2025/12/11 18:29:05 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks))
{
while (i > 0)
{
f(stacks);
i--;
}
}
void optimal_rotate(t_stacks *stacks, int i, int len, char stack)
{
if (i && len / i >= 2)
{
if (stack == 'a')
iterate_fn(stacks, i, &ra);
else
iterate_fn(stacks, i, &rb);
}
else
{
if (stack == 'a')
iterate_fn(stacks, len - i, &rra);
else
iterate_fn(stacks, len - i, &rrb);
}
}