Add index in t_stack + indexer function to add it

This commit is contained in:
2026-01-14 11:15:31 +01:00
parent 82111f59ad
commit 52220b5b49
9 changed files with 91 additions and 2 deletions

31
.clang-format Normal file
View File

@@ -0,0 +1,31 @@
Language: C
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlinesLeft: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
ColumnLimit: 80
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakString: 1
PenaltyExcessCharacter: 0
SpaceAfterCStyleCast: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Always

View File

@@ -33,7 +33,7 @@ INSERTION = insertion.c
FLAGS_FILES = algorithms_sort.c flag.c bench.c
PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c ft_itoa.c ft_isdigit.c \
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c indexer.c
CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c

BIN
checker_linux Executable file

Binary file not shown.

View File

@@ -34,5 +34,6 @@ int ft_strlcat(char *dst, const char *src, int size);
char *join_all(int argc, char **argv);
char **split_all(char *tab);
float compute_disorder(char **strs, int pos);
void indexer(t_stacks *stacks);
#endif

View File

@@ -16,6 +16,7 @@
typedef struct s_stack
{
int value;
long long index;
struct s_stack *next;
struct s_stack *previous;
} t_stack;

2
main.c
View File

@@ -32,7 +32,7 @@ int main(int argc, char **argv)
if (check_error(tab, mod))
test1(tab, len, mod);
else
write(2, "Error\n", 7);
write(2, "Error\n", 6);
free_tab(tab);
return (0);
}

54
parsing/indexer.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* indexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/14 10:14:06 by dgaillet #+# #+# */
/* Updated: 2026/01/14 10:14:08 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_stack *first_without_index(t_stack *stack)
{
t_stack *first;
if (stack->index < 0)
return (stack);
first = stack;
stack = stack->next;
while (stack != first)
{
if (stack->index < 0)
return (stack);
stack = stack->next;
}
return ((void *) 0);
}
void indexer(t_stacks *stacks)
{
int i;
int len;
t_stack *lower;
t_stack *temp;
i = 0;
len = stack_a_len(stacks);
while (i < len)
{
lower = first_without_index(stacks->a);
temp = lower->next;
while (temp != stacks->a)
{
if (lower->value > temp->value && temp->index < 0)
lower = temp;
temp = temp->next;
}
lower->index = i;
i++;
}
}

View File

@@ -21,6 +21,7 @@ t_stack *new_stack(int value)
if (!new)
return (NULL);
new->value = value;
new->index = -1;
new->next = NULL;
new->previous = NULL;
return (new);

View File

@@ -23,6 +23,7 @@ int test1(char **tab, int len, int mod)
if (mod == -1)
return (0);
stacks = init_stacks(len, tab, mod);
indexer(stacks);
if (!stacks)
return (0);
if (check_order(stacks->a))