add print stacks function

This commit is contained in:
David Gailleton
2025-12-09 13:38:10 +01:00
parent ad4275c206
commit c781d6f5cd
5 changed files with 101 additions and 23 deletions

View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:56:23 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

11
main.c
View File

@@ -6,7 +6,7 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 18:32:35 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->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B'); print_all_stack(stacks->b, stacks->b, 'B');
*/ */
print_all_stack(stacks->a, stacks->a, 'A'); //bubble_alg(stacks);
print_all_stack(stacks->b, stacks->b, 'B'); print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
bubble_alg(stacks); pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A'); print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
print_all_stack(stacks->b, stacks->b, 'B');
} }
stack_clear_all(stacks->a, stacks->a); stack_clear_all(stacks->a, stacks->a);
} }

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:18:06 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_stack *parsing(int argc, char **argv);
t_stacks *init_big_stacks(int argc, char **argv); t_stacks *init_big_stacks(int argc, char **argv);
int ft_atoi(const char *nptr); 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*/ /*ALGORITHM UTILS*/
int check_order(t_stack *stack); int check_order(t_stack *stack);
/*ALGORITHMS*/ /*ALGORITHMS*/
void bubble_alg(t_stacks *stacks); void bubble_alg(t_stacks *stacks);
#endif #endif

View File

@@ -1,30 +1,42 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* stack_len.c :+: :+: :+: */ /* print_stacks.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 12:05:15 by mteriier #+# #+# */ /* Created: 2025/12/09 13:10:00 by dgaillet #+# #+# */
/* Updated: 2025/12/09 12:07:29 by mteriier ### ########lyon.fr */ /* Updated: 2025/12/09 13:36:03 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <stdio.h>
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; int i;
first = stack; i = 0;
if (!stack) a_len = stack_a_len(stacks);
return (0); b_len = stack_b_len(stacks);
i = 1; while (i < len)
while (stack->next != first)
{ {
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++; i++;
} }
return (i); printf("_\t_\nA\tB\n\n");
} }

63
stack_utils/stacks_len.c Normal file
View File

@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stacks_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}