From 95577e5f0238eff161c2699e690c83898b0b70c2 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 7 Jan 2026 10:06:38 +0100 Subject: [PATCH] finish to link all the files --- Makefile | 16 +++- algorithms/medium/medium_algo.c | 1 + algorithms/medium/sort_utils.c | 75 ++++++++-------- algorithms/medium/utils_medium_two.c | 7 +- algorithms/medium/utils_struct_tab.c | 79 ++++++++--------- algorithms/utils/compare_value.c | 20 ----- algorithms/utils/iterate.c | 2 +- headers/medium_headers.h | 14 ++- headers/parsing.h | 7 ++ headers/push_swap.h | 123 ++++++++------------------- headers/simple_headers.h | 13 --- main.c | 73 +--------------- parsing/parsing.c | 3 +- parsing/parsing_2.c | 16 +--- test_one.c | 28 +----- 15 files changed, 158 insertions(+), 319 deletions(-) delete mode 100644 algorithms/utils/compare_value.c delete mode 100644 headers/simple_headers.h diff --git a/Makefile b/Makefile index cbfbf61..4d233cf 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,8 @@ STACK_UTILS_DIR = stack_utils ALGO_UTILS_DIR = algorithms/utils +INSERT_DIR = algorithms/insertion + ALGO_DIR = algorithms PARS_DIR = parsing @@ -20,26 +22,29 @@ INCLUDES = headers SRC = main.c test_one.c +INSERTION = insertion.c + PARSING = ft_atoi.c parsing.c parsing_2.c -STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c swap.c +STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c -ALGO_UTILS = check_order.c compare_value.c +ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c #============================ # ADDING PATH TO THE FILES #============================ ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \ - $(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) + $(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \ + $(INSERT_DIR)/$(INSERTION) OBJ_DIR = obj CC = cc -CFLAGS = -Wall -Werror -Wextra -I $(INCLUDES) +CFLAGS = -Wall -Werror -Wextra -I$(INCLUDES) NAME = push_swap @@ -62,6 +67,9 @@ $(OBJ_DIR)/%.o: %.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ +$(OBJ_DIR)/%.o: $(INSERT_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + $(OBJ_DIR)/%.o: $(STACK_UTILS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ diff --git a/algorithms/medium/medium_algo.c b/algorithms/medium/medium_algo.c index dec4805..ab0cee1 100644 --- a/algorithms/medium/medium_algo.c +++ b/algorithms/medium/medium_algo.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "medium_headers.h" #include static int path_to_end(t_stacks *piles, int max_range, int range, char c) diff --git a/algorithms/medium/sort_utils.c b/algorithms/medium/sort_utils.c index 9befa41..afea4b4 100644 --- a/algorithms/medium/sort_utils.c +++ b/algorithms/medium/sort_utils.c @@ -11,43 +11,9 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "medium_headers.h" -void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range) -{ - while (one_preset->nb_in > 0) - { - if (wich_path(piles, one_preset->max_range, range, 'b')) - { - while (!in_range(piles->b->value, one_preset->max_range, range)) - rb(piles); - } - else - { - while (!in_range(piles->b->value, one_preset->max_range, range)) - { - rrb(piles); - } - } - sort_little_pile(piles); - one_preset->nb_in--; - } -} - -int sort_path(t_stacks *piles, int value) -{ - int start_path; - int end_path; - - start_path = number_move_reverse(piles, value, 's'); - if (start_path == 0) - return (1); - end_path = number_move_reverse(piles, value, 'e'); - if (start_path < end_path) - return (1); - return (0); -} - -int number_move_reverse(t_stacks *piles, int value, char start_end) +static int number_move_reverse(t_stacks *piles, int value, char start_end) { int i; t_stack *tmp; @@ -74,7 +40,21 @@ int number_move_reverse(t_stacks *piles, int value, char start_end) return (i); } -void sort_little_pile(t_stacks *piles) +static int sort_path(t_stacks *piles, int value) +{ + int start_path; + int end_path; + + start_path = number_move_reverse(piles, value, 's'); + if (start_path == 0) + return (1); + end_path = number_move_reverse(piles, value, 'e'); + if (start_path < end_path) + return (1); + return (0); +} + +static void sort_little_pile(t_stacks *piles) { if (!piles->a) { @@ -92,3 +72,24 @@ void sort_little_pile(t_stacks *piles) else sort_from_right(piles); } + +void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range) +{ + while (one_preset->nb_in > 0) + { + if (wich_path(piles, one_preset->max_range, range, 'b')) + { + while (!in_range(piles->b->value, one_preset->max_range, range)) + rb(piles); + } + else + { + while (!in_range(piles->b->value, one_preset->max_range, range)) + { + rrb(piles); + } + } + sort_little_pile(piles); + one_preset->nb_in--; + } +} \ No newline at end of file diff --git a/algorithms/medium/utils_medium_two.c b/algorithms/medium/utils_medium_two.c index 9af03ab..8ff6ad6 100644 --- a/algorithms/medium/utils_medium_two.c +++ b/algorithms/medium/utils_medium_two.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "medium_headers.h" -int get_max_number(t_stack *first) +static int get_max_number(t_stack *first) { int max; t_stack *a; @@ -28,7 +29,7 @@ int get_max_number(t_stack *first) return (max); } -int get_min_number(t_stack *first) +static int get_min_number(t_stack *first) { int min; t_stack *a; @@ -44,7 +45,7 @@ int get_min_number(t_stack *first) return (min); } -int my_sqrt(int nb) +static int my_sqrt(int nb) { int i; diff --git a/algorithms/medium/utils_struct_tab.c b/algorithms/medium/utils_struct_tab.c index e9ec8ab..8114489 100644 --- a/algorithms/medium/utils_struct_tab.c +++ b/algorithms/medium/utils_struct_tab.c @@ -11,9 +11,10 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "medium_headers.h" #include -t_tab *allocate_tab(int range_max, int nb) +static t_tab *allocate_tab(int range_max, int nb) { t_tab *tab; @@ -26,6 +27,45 @@ t_tab *allocate_tab(int range_max, int nb) return (tab); } +static t_tab *free_tab(t_tab **first) +{ + if (!(*first)) + return (NULL); + if ((*first)->next) + free_tab(&(*first)->next); + free(*first); + return (NULL); +} + +static t_tab *init_first_tab(t_stack *first, int range) +{ + t_tab *tab; + int lower; + int range_max; + + lower = get_first_lower(first); + range_max = calcul_range(lower, range); + tab = allocate_tab(range_max, get_number_in_range(range_max, first, range)); + if (!tab) + return (NULL); + return (tab); +} + +static t_tab *get_next_tab(t_stack *first, t_tab *tab, int range) +{ + int lower; + int range_max; + t_tab *next_tab; + + lower = get_next_lower(first, tab->max_range); + range_max = calcul_range(lower, range); + next_tab = allocate_tab(range_max, + get_number_in_range(range_max, first, range)); + if (!next_tab) + return (NULL); + return (next_tab); +} + t_tab *get_tabs(t_stack *first, int range) { t_tab *tmp; @@ -49,40 +89,3 @@ t_tab *get_tabs(t_stack *first, int range) } return (first_tab); } - -t_tab *init_first_tab(t_stack *first, int range) -{ - t_tab *tab; - int lower; - int range_max; - - lower = get_first_lower(first); - range_max = calcul_range(lower, range); - tab = allocate_tab(range_max, get_number_in_range(range_max, first, range)); - if (!tab) - return (NULL); - return (tab); -} - -t_tab *get_next_tab(t_stack *first, t_tab *tab, int range) -{ - int lower; - int range_max; - t_tab *next_tab; - - lower = get_next_lower(first, tab->max_range); - range_max = calcul_range(lower, range); - next_tab = allocate_tab(range_max, - get_number_in_range(range_max, first, range)); - return (next_tab); -} - -t_tab *free_tab(t_tab **first) -{ - if (!(*first)) - return (NULL); - if ((*first)->next) - free_tab(&(*first)->next); - free(*first); - return (NULL); -} diff --git a/algorithms/utils/compare_value.c b/algorithms/utils/compare_value.c deleted file mode 100644 index 38d8db0..0000000 --- a/algorithms/utils/compare_value.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* compare_value.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mteriier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/12/10 11:08:35 by mteriier #+# #+# */ -/* Updated: 2025/12/10 11:28:58 by mteriier ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -int is_upper_compare(t_stack *t1, t_stack *t2) -{ - if (t1->value > t2->value) - return (1); - return (0); -} diff --git a/algorithms/utils/iterate.c b/algorithms/utils/iterate.c index cc38bdd..eaa9260 100644 --- a/algorithms/utils/iterate.c +++ b/algorithms/utils/iterate.c @@ -12,7 +12,7 @@ #include "push_swap.h" -void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks)) +static void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks)) { while (i > 0) { diff --git a/headers/medium_headers.h b/headers/medium_headers.h index 0c0babc..a340bed 100644 --- a/headers/medium_headers.h +++ b/headers/medium_headers.h @@ -24,10 +24,18 @@ typedef struct s_tab int wich_path(t_stacks *piles, int max_range, int range, char c); int stack_len(t_stack *stack); void bucket_algo(t_stacks *piles, t_tab *preset, int range); -/* SORT UTILS 2 FILE */ +/* SORT UTILS FILES */ void sort_from_left(t_stacks *piles); void sort_from_right(t_stacks *piles); -/* SORT UTILS FILE */ - +void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range); +/* MEDIUM UTILS FILES */ +int range_bucket(t_stack *first); +int get_first_lower(t_stack *first); +int get_next_lower(t_stack *first, int old_lower); +int calcul_range(int value, int range); +int in_range(int value, int max_range, int range); +int get_number_in_range(int max_range, t_stack *a, int range); +/* UTILS STRUCT TAB FILE */ +t_tab *get_tabs(t_stack *first, int range); #endif diff --git a/headers/parsing.h b/headers/parsing.h index e4368d8..a42d78e 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -10,4 +10,11 @@ /* */ /* ************************************************************************** */ +#ifndef PARSING_H +# define PARSING_H +int ft_atoi(const char *nptr); +t_stacks *init_big_stacks2(int *tab, int len); +t_stacks *init_big_stacks(int argc, char **argv); + +#endif diff --git a/headers/push_swap.h b/headers/push_swap.h index e08ce73..f38f1b3 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -26,92 +26,43 @@ typedef struct s_stacks t_stack *b; } t_stacks; -typedef struct s_tab -{ - int max_range; - int nb_in; - struct s_tab *next; -} t_tab; - +/* PRINT STACK FUNCTION*/ +void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b); /*STACK_FUNCTIONS*/ -void pa(t_stacks *stacks); -void pb(t_stacks *stacks); -void rra(t_stacks *stacks); -void rrb(t_stacks *stacks); -void rrr(t_stacks *stacks); -void ra(t_stacks *stacks); -void rb(t_stacks *stacks); -void rr(t_stacks *stacks); -void sa(t_stacks *stacks); -void sb(t_stacks *stacks); -void ss(t_stacks *stacks); - -/*FUNCTION UTILS*/ -t_stack *new_stack(int value); -void stack_add_back(t_stack **stack, t_stack *new); -void stack_add_front(t_stack **stack, t_stack *new); -void stack_clear_all(t_stack *stack, t_stack *first); -t_stack *parsing(int argc, char **argv); -t_stacks *init_big_stacks(int argc, char **argv); -int ft_atoi(const char *nptr); -/*FUNCTION UTILS ALGO*/ -int check_order(t_stack *stack); -int is_upper_compare(t_stack *t1, t_stack *t2); -int stack_len(t_stack *stack); -/*FUNCTION FOR MEDIUM ALGO*/ -t_tab *get_tabs(t_stack *first, int range); -int get_number_in_range(int max_range, t_stack *a, int range); -int in_range(int value, int max_range, int range); -int calcul_range(int value, int range); -int get_next_lower(t_stack *first, int old_lower); -int get_first_lower(t_stack *first); -int my_sqrt(int nb); -int get_max_number(t_stack *first); -int get_min_number(t_stack *first); -int range_bucket(t_stack *first); -t_tab *free_tab(t_tab **first); -t_tab *get_next_tab(t_stack *first, t_tab *tab, int range); -t_tab *init_first_tab(t_stack *first, int range); -t_tab *allocate_tab(int range_max, int nb); -int wich_path(t_stacks *piles, int max_range, int range, char c); -int path_to_start(t_stacks *piles, int max_range, int range, char c); -int path_to_end(t_stacks *piles, int max_range, int range, char c); -void bucket_algo(t_stacks *piles, t_tab *preset, int range); -void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range); -int sort_path(t_stacks *piles, int value); -int number_move_reverse(t_stacks *piles, int value, char start_end); -void sort_little_pile(t_stacks *piles); -void sort_from_left(t_stacks *piles); -void sort_from_right(t_stacks *piles); -/*FUNCTION IN MAIN*/ -void print_all_stack(t_stack *stack, t_stack *first, char pile); -int verif_no_double(int *tab, int len, int value); -int adding_number(int *tab, int len); -int *auto_shuffle(int len_tab); -/*FUNCTION IN FILE TEST*/ -int test1(int argc, char **argv); -int test2(char **argv); -/*FUNCTION IN PARSIN 2*/ -t_stack *parsing2(int *tab, int len); -t_stacks *init_big_stacks2(int *tab, int len); -void print_tabs(t_tab *preset); -void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b); -int stack_a_len(t_stacks *stacks); -int stack_b_len(t_stacks *stacks); -int highest_stack_len(t_stacks *stacks); - -/*ALGORITHM UTILS*/ -int check_order(t_stack *stack); -void iterate_fn(t_stacks *stacks, int i, void (f)(t_stacks *stacks)); -int is_lowest(t_stack *stack, t_stack *node, int len); -int is_highest(t_stack *stack, t_stack *node, int len); -void optimal_rotate(t_stacks *stacks, int i, int len, char stack); -int r_to_lowest(t_stack *stack, int len); -void sort_three_a(t_stacks *stacks); - -/*ALGORITHMS*/ -void bubble_alg(t_stacks *stacks); -//void insertion(t_stacks *stacks, int a_len, int b_len); -void insertion(t_stacks *stacks, int len); +void pa(t_stacks *stacks); +void pb(t_stacks *stacks); +void rra(t_stacks *stacks); +void rrb(t_stacks *stacks); +void rrr(t_stacks *stacks); +void ra(t_stacks *stacks); +void rb(t_stacks *stacks); +void rr(t_stacks *stacks); +void sa(t_stacks *stacks); +void sb(t_stacks *stacks); +void ss(t_stacks *stacks); +/* STACK ADD AND CLEAR FILES */ +t_stack *new_stack(int value); +void stack_add_back(t_stack **stack, t_stack *new); +void stack_add_front(t_stack **stack, t_stack *new); +void stack_clear_all(t_stack *stack, t_stack *first); +/* STACKS LEN FILES */ +int stack_a_len(t_stacks *stacks); +int stack_b_len(t_stacks *stacks); +int highest_stack_len(t_stacks *stacks); +/* PRE SORT */ +int r_to_lowest(t_stack *stack, int len); +void sort_three_a(t_stacks *stacks); +/* ITERATE FILE */ +void optimal_rotate(t_stacks *stacks, int i, int len, char stack); +/* COMPARE FILE */ +int is_lowest(t_stack *stack, t_stack *node, int len); +int is_highest(t_stack *stack, t_stack *node, int len); +/* CHECK ORDER FILE */ +int check_order(t_stack *stack); +/* INSERTION */ +void insertion(t_stacks *stacks, int len); +int test2(char **argv); +/* TEST FILE */ +int test1(int argc, char **argv); #endif diff --git a/headers/simple_headers.h b/headers/simple_headers.h deleted file mode 100644 index e747ae6..0000000 --- a/headers/simple_headers.h +++ /dev/null @@ -1,13 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* simple_headers.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mteriier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2026/01/07 07:47:59 by mteriier #+# #+# */ -/* Updated: 2026/01/07 07:48:02 by mteriier ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - - diff --git a/main.c b/main.c index a29111f..da33874 100644 --- a/main.c +++ b/main.c @@ -11,81 +11,10 @@ /* ************************************************************************** */ #include "push_swap.h" -#include -#include -#include - -void print_all_stack(t_stack *stack, t_stack *first, char pile) -{ - t_stack *tmp; - int i; - - tmp = stack; - i = 0; - printf("TAB %c : \n", pile); - if (!stack || !first) - return ; - while (tmp->next != first) - { - printf("[%d] ", tmp->value); - tmp = tmp->next; - i++; - } - printf("[%d] \n", tmp->value); -} - -int verif_no_double(int *tab, int len, int value) -{ - int i; - - i = 0; - while (i < len) - { - if (value == tab[i]) - return (0); - i++; - } - return (1); -} - -int adding_number(int *tab, int len) -{ - int stock; - - stock = tab[0]; - while (!verif_no_double(tab, len, stock)) - { - stock = 0 + rand() % (250 - 0 + 1); - } - return (stock); -} - -int *auto_shuffle(int len_tab) -{ - int *tab; - int i; - - i = 1; - tab = malloc(len_tab * sizeof(int)); - if (!tab) - return (NULL); - tab[0] = 0 + rand() % (250 - 0 + 1); - while (i < len_tab) - { - tab[i] = adding_number(tab, i - 1); - i++; - } - return (tab); -} int main(int argc, char **argv) { if (argc > 1) - { - if (strcmp(argv[1], "-t2") == 0) - test2(argv++); - else - test1(argc, argv); - } + test1(argc, argv); return (0); } diff --git a/parsing/parsing.c b/parsing/parsing.c index 786cfb3..7a7c0f6 100644 --- a/parsing/parsing.c +++ b/parsing/parsing.c @@ -11,9 +11,10 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "parsing.h" #include -t_stack *parsing(int argc, char **argv) +static t_stack *parsing(int argc, char **argv) { int i; int stock; diff --git a/parsing/parsing_2.c b/parsing/parsing_2.c index 1eeeeb0..b1f653f 100644 --- a/parsing/parsing_2.c +++ b/parsing/parsing_2.c @@ -12,22 +12,8 @@ #include "push_swap.h" #include -#include -void print_tabs(t_tab *preset) -{ - t_tab *tab; - - tab = preset; - while (tab) - { - printf("MAX RANGE : [%d]\n", tab->max_range); - printf("NUMBER IN : [%d]\n", tab->nb_in); - tab = tab->next; - } -} - -t_stack *parsing2(int *tab, int len) +static t_stack *parsing2(int *tab, int len) { int i; int stock; diff --git a/test_one.c b/test_one.c index da1dac3..c710be1 100644 --- a/test_one.c +++ b/test_one.c @@ -11,6 +11,8 @@ /* ************************************************************************** */ #include "push_swap.h" +#include "parsing.h" +#include "medium_headers.h" #include #include @@ -33,29 +35,3 @@ int test1(int argc, char **argv) free(piles); return (0); } - -int test2(char **argv) -{ - int *tab; - int len; - t_tab *preset; - t_stacks *piles; - - len = ft_atoi(argv[2]); - if (len < 1) - { - printf("WRONG LEN PLS BE SMART.\n"); - return (0); - } - tab = auto_shuffle(len); - piles = init_big_stacks2(tab, len); - preset = get_tabs(piles->a, range_bucket(piles->a)); - bucket_algo(piles, preset, range_bucket(piles->a)); - free(tab); - if (piles->a) - stack_clear_all(piles->a, piles->a); - if (piles->b) - stack_clear_all(piles->b, piles->b); - free(piles); - return (0); -}