10 Commits

Author SHA1 Message Date
David Gailleton
e9c90e447c more optimized insertion sort algorithm 2025-12-14 17:31:36 +01:00
David Gailleton
0d01b58ff1 insertion sort note optimized is working 2025-12-14 16:51:11 +01:00
David Gailleton
a68bcd2503 Merge branch 'master' into simple_algorithm 2025-12-12 11:40:58 +01:00
David Gailleton
55363e0e6f Fix header file includes AND add write for each push swap functions 2025-12-12 11:40:02 +01:00
David Gailleton
f5da9fac1f New insertion algorithm WIP 2025-12-12 11:32:01 +01:00
David Gailleton
1463f3e14a bubble v1 2025-12-09 14:23:22 +01:00
David Gailleton
c781d6f5cd add print stacks function 2025-12-09 13:38:10 +01:00
Maoake Teriierooiterai
ad4275c206 bubble algorithm WIP 2025-12-09 12:43:28 +01:00
Maoake Teriierooiterai
c9066f4a9b Algorithm firsts utils (stack size + check order) 2025-12-09 12:32:13 +01:00
Maoake Teriierooiterai
8cd29f7151 all basics functions has been tested we can start the sort algorithms 2025-12-09 11:45:42 +01:00
17 changed files with 523 additions and 54 deletions

3
.gitignore vendored
View File

@@ -53,3 +53,6 @@ dkms.conf
# debug information files # debug information files
*.dwo *.dwo
# Vim swap files
*.swp

View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bubble.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:56:23 by mteriier #+# #+# */
/* Updated: 2025/12/09 15:56:56 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "unistd.h"
void bubble_alg(t_stacks *stacks)
{
if (check_order(stacks->a))
return ;
while (stack_a_len(stacks))
{
if (stacks->a->value > stacks->a->next->value)
{
sa(stacks);
write(1, "sa\n", 3);
}
pb(stacks);
write(1, "pb\n", 3);
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
}
while (stack_b_len(stacks))
{
if (stacks->b->value < stacks->b->next->value)
{
sb(stacks);
write(1, "sa\n", 3);
}
pa(stacks);
write(1, "pb\n", 3);
// print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
}
bubble_alg(stacks);
}

View File

@@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* insertion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 15:38:52 by dgaillet #+# #+# */
/* Updated: 2025/12/14 17:31:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int r_to_lowest(t_stack *stack, int len)
{
t_stack *lowest;
int lowest_i;
int i;
i = 1;
lowest_i = 0;
lowest = stack;
stack = stack->next;
while (i < len)
{
if (stack->value < lowest->value)
{
lowest_i = i;
lowest = stack;
}
stack = stack->next;
i++;
}
return (lowest_i);
}
static int to_insert(t_stacks *stacks, int sorted)
{
int i;
t_stack *a;
if (is_lowest(stacks->a, stacks->b, sorted)
|| is_highest(stacks->a, stacks->b, sorted))
return (sorted - r_to_lowest(stacks->a, sorted));
i = 0;
a = stacks->a;
while (i < sorted)
{
if (stacks->b->value > a->previous->value && stacks->b->value <= a->value)
return (i);
a = a->previous;
i++;
}
return (i);
}
static void ft_extra(t_stacks *stacks, int sorted, int b_len)
{
int to_r_rotate;
if (b_len <= 0)
return ;
if (sorted <= 1)
pa(stacks);
else
{
to_r_rotate = to_insert(stacks, sorted);
optimal_rotate(stacks, sorted - to_r_rotate, sorted, 'a');
pa(stacks);
}
ft_extra(stacks, sorted + 1, b_len - 1);
}
void insertion(t_stacks *stacks, int len)
{
int i;
i = 0;
while (i < len)
{
pb(stacks);
i++;
}
ft_extra(stacks, 0, len);
optimal_rotate(stacks, r_to_lowest(stacks->a, len), len, 'a');
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_order.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 11:57:06 by mteriier #+# #+# */
/* Updated: 2025/12/11 17:50:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int check_order(t_stack *stack)
{
t_stack *first;
first = stack;
while (stack->next != first)
{
if (stack->value > stack->next->value)
return (0);
stack = stack->next;
}
return (1);
}

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);
}
}

49
main.c
View File

@@ -6,35 +6,70 @@
/* 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/08 20:36:49 by mteriier ### ########.fr */ /* Updated: 2025/12/14 16:55:43 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
void print_all_stack(t_stack *stack, t_stack *first) void print_all_stack(t_stack *stack, t_stack *first, char pile)
{ {
t_stack *tmp; t_stack *tmp;
int i;
tmp = stack; tmp = stack;
printf("TAB\n"); i = 0;
printf("TAB %c : \n", pile);
if (!stack || !first)
return ;
while (tmp->next != first) while (tmp->next != first)
{ {
printf("[%d] ", tmp->value); printf("[%d] ", tmp->value);
tmp = tmp->next; tmp = tmp->next;
i++;
} }
printf("[%d] \n", tmp->value); printf("[%d] \n", tmp->value);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_stack *first; t_stacks *stacks;
stacks = NULL;
if (argc > 1) if (argc > 1)
{ {
first = parsing(argc, argv); stacks = init_big_stacks(argc, argv);
print_all_stack(first, first); /* print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
sa(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pa(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rra(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pb(stacks);
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrr(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
*/
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
insertion(stacks, stack_a_len(stacks));
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
} }
stack_clear_all(first, first); stack_clear_all(stacks->a, stacks->a);
stack_clear_all(stacks->b, stacks->b);
} }

View File

@@ -6,17 +6,16 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */ /* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */
/* Updated: 2025/12/08 20:31:37 by mteriier ### ########.fr */ /* Updated: 2025/12/09 10:19:17 by mteriier ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
t_stack *parsing(int argc, char **argv) t_stack *parsing(int argc, char **argv)
{ {
size_t i; int i;
int stock; int stock;
t_stack *first; t_stack *first;
t_stack *new; t_stack *new;
@@ -39,3 +38,16 @@ t_stack *parsing(int argc, char **argv)
} }
return (first); return (first);
} }
t_stacks *init_big_stacks(int argc, char **argv)
{
t_stacks *stacks;
t_stack *a;
stacks = malloc(sizeof(t_stacks));
if (!stacks)
return (NULL);
a = parsing(argc, argv);
stacks->a = a;
return (stacks);
}

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/08 20:28:41 by mteriier ### ########.fr */ /* Updated: 2025/12/14 16:55:24 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -27,23 +27,41 @@ typedef struct s_stacks
} t_stacks; } t_stacks;
/*STACK_FUNCTIONS*/ /*STACK_FUNCTIONS*/
void pa(t_stacks *stacks); void pa(t_stacks *stacks);
void pb(t_stacks *stacks); void pb(t_stacks *stacks);
void rra(t_stacks *stacks); void rra(t_stacks *stacks);
void rrb(t_stacks *stacks); void rrb(t_stacks *stacks);
void rrr(t_stacks *stacks); void rrr(t_stacks *stacks);
void ra(t_stacks *stacks); void ra(t_stacks *stacks);
void rb(t_stacks *stacks); void rb(t_stacks *stacks);
void rr(t_stacks *stacks); void rr(t_stacks *stacks);
void sa(t_stacks *stacks); void sa(t_stacks *stacks);
void sb(t_stacks *stacks); void sb(t_stacks *stacks);
void ss(t_stacks *stacks); void ss(t_stacks *stacks);
/*FUNCTION UTILS*/ /*FUNCTION UTILS*/
t_stack *new_stack(int value); t_stack *new_stack(int value);
void stack_add_back(t_stack **stack, t_stack *new); void stack_add_back(t_stack **stack, t_stack *new);
void stack_add_front(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); void stack_clear_all(t_stack *stack, t_stack *first);
t_stack *parsing(int argc, char **argv); t_stack *parsing(int argc, char **argv);
int ft_atoi(const char *nptr); 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);
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);
/*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);
#endif #endif

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_stacks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.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 <stdio.h>
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b)
{
int a_len;
int b_len;
int i;
i = 0;
a_len = stack_a_len(stacks);
b_len = stack_b_len(stacks);
while (i < len)
{
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++;
}
printf("_\t_\nA\tB\n\n");
}

View File

@@ -6,28 +6,50 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */ /* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */
/* Updated: 2025/12/08 19:37:01 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:33 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <stdlib.h>
#include <unistd.h>
void pa(t_stacks *stacks) void pa(t_stacks *stacks)
{ {
t_stack *a; t_stack *b_push;
if (!stacks || !stacks->b) if (!stacks || !stacks->b)
return ; return ;
a = stacks->a; b_push = stacks->b;
a->value = stacks->b->value; if (stacks->b->next == stacks->b)
stacks->b = NULL;
else
{
stacks->b->next->previous = stacks->b->previous;
stacks->b->previous->next = stacks->b->next;
stacks->b = stacks->b->next;
}
stack_add_front(&(stacks->a), b_push);
write(1, "pa\n", 3);
} }
#include <stdio.h>
void pb(t_stacks *stacks) void pb(t_stacks *stacks)
{ {
t_stack *b; t_stack *a_push;
if (!stacks || !stacks->a) if (!stacks || !stacks->a)
return ; return ;
b = stacks->b; a_push = stacks->a;
b->value = stacks->a->value; if (stacks->a->next == stacks->a)
stacks->a = NULL;
else
{
stacks->a->next->previous = stacks->a->previous;
stacks->a->previous->next = stacks->a->next;
stacks->a = stacks->a->next;
}
stack_add_front(&(stacks->b), a_push);
write(1, "pb\n", 3);
} }

View File

@@ -6,26 +6,32 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */ /* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:37:26 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:25 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <unistd.h>
void rra(t_stacks *stacks) void rra(t_stacks *stacks)
{ {
if (stacks && stacks->a && stacks->a->previous) if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous; stacks->a = stacks->a->previous;
write(1, "rra\n", 4);
} }
void rrb(t_stacks *stacks) void rrb(t_stacks *stacks)
{ {
if (stacks && stacks->b && stacks->b->previous) if (stacks && stacks->b && stacks->b->previous)
stacks->b = stacks->b->previous; stacks->b = stacks->b->previous;
write(1, "rrb\n", 4);
} }
void rrr(t_stacks *stacks) void rrr(t_stacks *stacks)
{ {
rra(stacks); if (stacks && stacks->b && stacks->b->previous)
rrb(stacks); stacks->b = stacks->b->previous;
if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous;
write(1, "rrr\n", 4);
} }

View File

@@ -6,26 +6,32 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */ /* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:37:44 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:17 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <unistd.h>
void ra(t_stacks *stacks) void ra(t_stacks *stacks)
{ {
if (stacks && stacks->a && stacks->a->next) if (stacks && stacks->a && stacks->a->next)
stacks->a = stacks->a->next; stacks->a = stacks->a->next;
write(1, "ra\n", 3);
} }
void rb(t_stacks *stacks) void rb(t_stacks *stacks)
{ {
if (stacks && stacks->b && stacks->b->next) if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next; stacks->b = stacks->b->next;
write(1, "rb\n", 3);
} }
void rr(t_stacks *stacks) void rr(t_stacks *stacks)
{ {
ra(stacks); if (stacks && stacks->a && stacks->a->next)
rb(stacks); stacks->a = stacks->a->next;
if (stacks && stacks->b && stacks->b->next)
stacks->b = stacks->b->next;
write(1, "rr\n", 3);
} }

View File

@@ -6,11 +6,11 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */ /* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */
/* Updated: 2025/12/08 20:33:58 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:09 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <stdlib.h> #include <stdlib.h>
t_stack *new_stack(int value) t_stack *new_stack(int value)
@@ -26,8 +26,6 @@ t_stack *new_stack(int value)
return (new); return (new);
} }
#include <stdio.h>
void stack_add_back(t_stack **stack, t_stack *new) void stack_add_back(t_stack **stack, t_stack *new)
{ {
if (!stack || !new) if (!stack || !new)

View File

@@ -6,11 +6,11 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 16:05:27 by dgaillet #+# #+# */ /* Created: 2025/12/08 16:05:27 by dgaillet #+# #+# */
/* Updated: 2025/12/08 19:40:01 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:39:02 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <stdlib.h> #include <stdlib.h>
void stack_clear_all(t_stack *stack, t_stack *first) void stack_clear_all(t_stack *stack, t_stack *first)

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);
}

View File

@@ -6,11 +6,12 @@
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */ /* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */ /* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */
/* Updated: 2025/12/08 19:40:49 by mteriier ### ########.fr */ /* Updated: 2025/12/12 11:38:52 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../push_swap.h" #include "push_swap.h"
#include <unistd.h>
void sa(t_stacks *stacks) void sa(t_stacks *stacks)
{ {
@@ -23,6 +24,7 @@ void sa(t_stacks *stacks)
stock = a->value; stock = a->value;
a->value = a->next->value; a->value = a->next->value;
a->next->value = stock; a->next->value = stock;
write(1, "sa\n", 3);
} }
void sb(t_stacks *stacks) void sb(t_stacks *stacks)
@@ -36,10 +38,30 @@ void sb(t_stacks *stacks)
stock = b->value; stock = b->value;
b->value = b->next->value; b->value = b->next->value;
b->next->value = stock; b->next->value = stock;
write(1, "sb\n", 3);
} }
void ss(t_stacks *stacks) void ss(t_stacks *stacks)
{ {
sa(stacks); t_stack *b;
sb(stacks); t_stack *a;
int stock;
if (!stacks)
return ;
if (stacks->b && stacks->b->next)
{
b = stacks->b;
stock = b->value;
b->value = b->next->value;
b->next->value = stock;
}
if (stacks->a && stacks->a->next)
{
a = stacks->a;
stock = a->value;
a->value = a->next->value;
a->next->value = stock;
}
write(1, "ss\n", 3);
} }