From c781d6f5cdb59bb2047606795866b0e8490584bf Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Tue, 9 Dec 2025 13:38:10 +0100 Subject: [PATCH] add print stacks function --- algorithms/bubble/bubble.c | 2 +- main.c | 11 ++-- push_swap.h | 10 ++- .../stack_len.c => stack_utils/print_stacks.c | 38 +++++++---- stack_utils/stacks_len.c | 63 +++++++++++++++++++ 5 files changed, 101 insertions(+), 23 deletions(-) rename algorithms/utils/stack_len.c => stack_utils/print_stacks.c (50%) create mode 100644 stack_utils/stacks_len.c diff --git a/algorithms/bubble/bubble.c b/algorithms/bubble/bubble.c index b396341..ac37d8d 100644 --- a/algorithms/bubble/bubble.c +++ b/algorithms/bubble/bubble.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:43:09 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:19:39 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ diff --git a/main.c b/main.c index 1327daa..7e77843 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:39:19 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:35:44 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,11 +66,10 @@ int main(int argc, char **argv) print_all_stack(stacks->a, stacks->a, 'A'); print_all_stack(stacks->b, stacks->b, 'B'); */ - print_all_stack(stacks->a, stacks->a, 'A'); - print_all_stack(stacks->b, stacks->b, 'B'); - bubble_alg(stacks); - print_all_stack(stacks->a, stacks->a, 'A'); - print_all_stack(stacks->b, stacks->b, 'B'); + //bubble_alg(stacks); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); + pb(stacks); + print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b); } stack_clear_all(stacks->a, stacks->a); } diff --git a/push_swap.h b/push_swap.h index b96637a..6fc2744 100644 --- a/push_swap.h +++ b/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/09 12:41:07 by mteriier ### ########lyon.fr */ +/* Updated: 2025/12/09 13:33:04 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -47,11 +47,15 @@ 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); +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); +int check_order(t_stack *stack); /*ALGORITHMS*/ -void bubble_alg(t_stacks *stacks); +void bubble_alg(t_stacks *stacks); #endif diff --git a/algorithms/utils/stack_len.c b/stack_utils/print_stacks.c similarity index 50% rename from algorithms/utils/stack_len.c rename to stack_utils/print_stacks.c index c379297..261fbd4 100644 --- a/algorithms/utils/stack_len.c +++ b/stack_utils/print_stacks.c @@ -1,30 +1,42 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* stack_len.c :+: :+: :+: */ +/* print_stacks.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: mteriier +#+ +:+ +#+ */ +/* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/12/09 12:05:15 by mteriier #+# #+# */ -/* Updated: 2025/12/09 12:07:29 by mteriier ### ########lyon.fr */ +/* Created: 2025/12/09 13:10:00 by dgaillet #+# #+# */ +/* Updated: 2025/12/09 13:36:03 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" +#include -int stack_len(t_stack *stack) +void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b) { - t_stack *first; + int a_len; + int b_len; int i; - first = stack; - if (!stack) - return (0); - i = 1; - while (stack->next != first) + i = 0; + a_len = stack_a_len(stacks); + b_len = stack_b_len(stacks); + while (i < len) { - stack = stack->next; + if (a_len >= len - i) + { + printf("%d", a->value); + a = a->next; + } + printf("\t"); + if (b_len >= len - i) + { + printf("%d", b->value); + b = b->next; + } + printf("\n"); i++; } - return (i); + printf("_\t_\nA\tB\n\n"); } diff --git a/stack_utils/stacks_len.c b/stack_utils/stacks_len.c new file mode 100644 index 0000000..6aa44f9 --- /dev/null +++ b/stack_utils/stacks_len.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stacks_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/09 13:11:50 by dgaillet #+# #+# */ +/* Updated: 2025/12/09 13:19:21 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int stack_a_len(t_stacks *stacks) +{ + t_stack *first; + t_stack *a_stack; + int len; + + a_stack = stacks->a; + first = a_stack; + if (!a_stack) + return (0); + len = 1; + while (a_stack->next != first) + { + a_stack = a_stack->next; + len++; + } + return (len); +} + +int stack_b_len(t_stacks *stacks) +{ + t_stack *first; + t_stack *b_stack; + int len; + + b_stack = stacks->b; + first = b_stack; + if (!b_stack) + return (0); + len = 1; + while (b_stack->next != first) + { + b_stack = b_stack->next; + len++; + } + return (len); +} + +int highest_stack_len(t_stacks *stacks) +{ + int a_len; + int b_len; + + a_len = stack_a_len(stacks); + b_len = stack_b_len(stacks); + if (a_len > b_len) + return (a_len); + return (b_len); +}