upgraded algorithme
This commit is contained in:
3
Makefile
3
Makefile
@ -6,7 +6,7 @@
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
|
||||
# Updated: 2024/02/21 17:40:53 by adjoly ### ########.fr #
|
||||
# Updated: 2024/02/22 11:51:16 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -16,6 +16,7 @@ CC = cc
|
||||
|
||||
SRCS = main.c \
|
||||
algo.c \
|
||||
median.c \
|
||||
check_error.c \
|
||||
print_stack.c \
|
||||
parsing.c \
|
||||
|
49
algo.c
49
algo.c
@ -6,13 +6,31 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/21 17:29:26 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/21 19:19:55 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:29:52 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "push_swap.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void send_to_stack_b(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t stack_size;
|
||||
int median;
|
||||
|
||||
while (*stack_a)
|
||||
{
|
||||
median = find_median(stack_a);
|
||||
stack_size = ft_stacksize(*stack_a);
|
||||
while (*stack_a && stack_size != 0)
|
||||
{
|
||||
if ((*stack_a)->nb < median)
|
||||
ft_push_b(stack_a, stack_b);
|
||||
else
|
||||
ft_rotatestack_a(stack_a);
|
||||
stack_size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t get_stack_max(t_stack **stack)
|
||||
{
|
||||
@ -42,20 +60,33 @@ void ft_algo(t_stack **stack_a, t_stack **stack_b)
|
||||
{
|
||||
size_t max;
|
||||
size_t i;
|
||||
size_t stack_size;
|
||||
|
||||
i = 0;
|
||||
while ((*stack_a))
|
||||
ft_push_b(stack_a, stack_b);
|
||||
send_to_stack_b(stack_a, stack_b);
|
||||
stack_size = ft_stacksize(*stack_b);
|
||||
while (*stack_b)
|
||||
{
|
||||
max = get_stack_max(stack_b);
|
||||
i = 0;
|
||||
while (i < max)
|
||||
if (max < stack_size / 2)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
i++;
|
||||
while (i < max)
|
||||
{
|
||||
ft_rotatestack_b(stack_b);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
i = stack_size;
|
||||
while (i > max)
|
||||
{
|
||||
ft_reverserotate_b(stack_b);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
ft_push_a(stack_a, stack_b);
|
||||
stack_size--;
|
||||
}
|
||||
}
|
||||
|
||||
|
2
libft
2
libft
Submodule libft updated: 41548b6c53...63a5504b82
2
main.c
2
main.c
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:14:22 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/21 17:29:15 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:28:28 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
44
median.c
Normal file
44
median.c
Normal file
@ -0,0 +1,44 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* median.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/22 11:27:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/22 13:27:41 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
t_boolean is_median(t_stack **stack, int nb)
|
||||
{
|
||||
t_stack *tmp;
|
||||
int count;
|
||||
|
||||
tmp = *stack;
|
||||
count = 0;
|
||||
while (tmp)
|
||||
{
|
||||
count += (tmp->nb < nb) - (tmp->nb > nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if (count == -1 || count == 0 || count == 1)
|
||||
return (TRUE);
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
int find_median(t_stack **stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
tmp = *stack;
|
||||
while (tmp)
|
||||
{
|
||||
if (is_median(stack, tmp->nb) == TRUE)
|
||||
return (tmp->nb);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (ERROR);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 12:18:29 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/21 17:39:21 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 11:50:24 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -52,5 +52,6 @@ void ft_reverserotate_b(t_stack **stack_b);
|
||||
void ft_reverserotate_r(t_stack **stack_a, t_stack **stack_b);
|
||||
|
||||
void ft_algo(t_stack **stack_a, t_stack **stack_b);
|
||||
int find_median(t_stack **stack);
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:40:39 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/05 11:42:15 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:32:31 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,4 +23,3 @@ void ft_stackadd_back(t_stack **stack, t_stack *new)
|
||||
}
|
||||
ft_stacklast(*stack)->next = new;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 18:52:38 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/05 11:38:54 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:32:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,4 +21,3 @@ void ft_stackadd_front(t_stack **stack, t_stack *new)
|
||||
new->next = *stack;
|
||||
*stack = new;
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 18:54:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/05 11:39:22 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:32:09 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,4 +24,3 @@ void ft_stackclear(t_stack **stack)
|
||||
*stack = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/06 10:27:14 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/06 10:28:24 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:31:53 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,4 +18,3 @@ void ft_stackdelone(t_stack *stack)
|
||||
return ;
|
||||
free(stack);
|
||||
}
|
||||
|
||||
|
@ -6,13 +6,13 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/06 10:18:01 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/13 13:40:36 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:33:16 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../push_swap.h"
|
||||
|
||||
t_stack *ft_stacklast(t_stack *stack)
|
||||
t_stack *ft_stacklast(t_stack *stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
@ -22,10 +22,9 @@ t_stack *ft_stacklast(t_stack *stack)
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
return (tmp);
|
||||
|
||||
}
|
||||
|
||||
t_stack *ft_stackbeforelast(t_stack *stack)
|
||||
t_stack *ft_stackbeforelast(t_stack *stack)
|
||||
{
|
||||
t_stack *tmp;
|
||||
|
||||
@ -35,6 +34,4 @@ t_stack *ft_stackbeforelast(t_stack *stack)
|
||||
while (tmp && tmp->next && tmp->next->next)
|
||||
tmp = tmp->next;
|
||||
return (tmp);
|
||||
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:45:19 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/05 14:11:46 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:31:23 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,4 +23,3 @@ t_stack *ft_stacknew(int nb)
|
||||
stack->next = NULL;
|
||||
return (stack);
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/05 11:42:40 by adjoly #+# #+# */
|
||||
/* Updated: 2024/02/05 11:44:21 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/02/22 13:29:39 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,4 +24,3 @@ size_t ft_stacksize(t_stack *stack)
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
|
@ -1,237 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void afficher_KO_OK(int i)
|
||||
{
|
||||
if (i == 1)
|
||||
printf("ok ");
|
||||
else
|
||||
printf("ERREUR ");
|
||||
}
|
||||
|
||||
/* si tu modif un prototype de fonction: modif la aussi*/
|
||||
typedef struct s_stack
|
||||
{
|
||||
int nb_init;
|
||||
int nb;
|
||||
struct s_stack *next;
|
||||
} t_stack;
|
||||
|
||||
t_stack *ft_stacknew(int content);
|
||||
int ft_stacksize(t_stack *stack);
|
||||
void ft_stackadd_back(t_stack **stack, t_stack *new);
|
||||
t_stack *ft_stacklast(t_stack *stack);
|
||||
void ft_stackadd_front(t_stack **stack, t_stack *new);
|
||||
|
||||
int ft_verif_nb(char const *str);
|
||||
t_stack *fill_struct(int ac, char **av);
|
||||
int verif_double(t_stack *stack);
|
||||
void replace_nb_init(t_stack *stack);
|
||||
int is_sorted(t_stack *stack_a);
|
||||
|
||||
|
||||
|
||||
void main_stacknew()
|
||||
{
|
||||
printf("stacknew: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
afficher_KO_OK(new->nb_init == 1000);
|
||||
afficher_KO_OK(new->next == NULL);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_stacksize()
|
||||
{
|
||||
printf("stacksize: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
afficher_KO_OK(ft_stacksize(new) == 1);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
new->next = new2;
|
||||
afficher_KO_OK(ft_stacksize(new) == 2);
|
||||
afficher_KO_OK(ft_stacksize(NULL) == 0);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_stackadd_back()
|
||||
{
|
||||
printf("stackadd_back: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
t_stack *new3 = ft_stacknew(2);
|
||||
ft_stackadd_back(&new, new2);
|
||||
ft_stackadd_back(&new, new3);
|
||||
afficher_KO_OK(ft_stacksize(new) == 3);
|
||||
afficher_KO_OK(new->nb_init == 1000);
|
||||
afficher_KO_OK(new->next->nb_init == 1);
|
||||
afficher_KO_OK(new->next->next->nb_init == 2);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_stacklast()
|
||||
{
|
||||
printf("stacklast: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
t_stack *new3 = ft_stacknew(2);
|
||||
new2->next = new3;
|
||||
new->next = new2;
|
||||
afficher_KO_OK(ft_stacklast(new)->nb_init == 2);
|
||||
afficher_KO_OK(ft_stacklast(new)->next == NULL);
|
||||
t_stack *new4 = ft_stacknew(6);
|
||||
new3->next = new4;
|
||||
afficher_KO_OK(ft_stacklast(new)->nb_init == 6);
|
||||
afficher_KO_OK(ft_stacklast(new)->next == NULL);
|
||||
afficher_KO_OK(ft_stacklast(new2)->nb_init == 6);
|
||||
afficher_KO_OK(ft_stacklast(new2)->next == NULL);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_stackadd_front()
|
||||
{
|
||||
printf("stackadd_front: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
t_stack *new3 = ft_stacknew(2);
|
||||
ft_stackadd_front(&new, new2);
|
||||
ft_stackadd_front(&new, new3);
|
||||
afficher_KO_OK(ft_stacksize(new) == 3);
|
||||
afficher_KO_OK(new->nb_init == 2);
|
||||
afficher_KO_OK(new->next->nb_init == 1);
|
||||
afficher_KO_OK(new->next->next->nb_init == 1000);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_stack()
|
||||
{
|
||||
main_stacknew();
|
||||
main_stacksize();
|
||||
main_stackadd_back();
|
||||
main_stacklast();
|
||||
main_stackadd_front();
|
||||
}
|
||||
|
||||
void main_verif_nb()
|
||||
{
|
||||
printf("verif_nb: ");
|
||||
afficher_KO_OK(ft_verif_nb("0") == 1);
|
||||
afficher_KO_OK(ft_verif_nb("1000") == 1);
|
||||
afficher_KO_OK(ft_verif_nb("-1000") == 1);
|
||||
afficher_KO_OK(ft_verif_nb("2147483647") == 1);
|
||||
afficher_KO_OK(ft_verif_nb("2147483648") == 0);
|
||||
afficher_KO_OK(ft_verif_nb("-2147483648") == 1);
|
||||
afficher_KO_OK(ft_verif_nb("-2147483649") == 0);
|
||||
afficher_KO_OK(ft_verif_nb("1234567890123456") == 0);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_fill_struct()
|
||||
{
|
||||
int ac = 4;
|
||||
char **av = (char *[]){"1 2 3", "4 5 6","7 8 9" "10"};
|
||||
t_stack *stack = fill_struct(ac, av);
|
||||
afficher_KO_OK(ft_stacksize(stack) == 10);
|
||||
afficher_KO_OK(stack->nb_init == 1 &&
|
||||
stack->next->nb_init == 2 &&
|
||||
stack->next->next->nb_init == 3 &&
|
||||
stack->next->next->next->nb_init == 4 &&
|
||||
stack->next->next->next->next->next->nb_init == 5 &&
|
||||
stack->next->next->next->next->next->next->nb_init == 6 &&
|
||||
ft_stacklast(stack)->nb_init == 10);
|
||||
|
||||
ac = 1;
|
||||
av = (char *[]){"1 2 3 4 5 6 7 8 9 10"};
|
||||
stack = fill_struct(ac, av);
|
||||
afficher_KO_OK(ft_stacksize(stack) == 10);
|
||||
afficher_KO_OK(stack->nb_init == 1 &&
|
||||
stack->next->nb_init == 2 &&
|
||||
stack->next->next->nb_init == 3 &&
|
||||
stack->next->next->next->nb_init == 4 &&
|
||||
stack->next->next->next->next->next->nb_init == 5 &&
|
||||
stack->next->next->next->next->next->next->nb_init == 6 &&
|
||||
ft_stacklast(stack)->nb_init == 10);
|
||||
|
||||
ac = 10;
|
||||
av = (char *[]){"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
|
||||
stack = fill_struct(ac, av);
|
||||
afficher_KO_OK(ft_stacksize(stack) == 10);
|
||||
afficher_KO_OK(stack->nb_init == 1 &&
|
||||
stack->next->nb_init == 2 &&
|
||||
stack->next->next->nb_init == 3 &&
|
||||
stack->next->next->next->nb_init == 4 &&
|
||||
stack->next->next->next->next->next->nb_init == 5 &&
|
||||
stack->next->next->next->next->next->next->nb_init == 6 &&
|
||||
ft_stacklast(stack)->nb_init == 10);
|
||||
|
||||
ac = 10;
|
||||
av = (char *[]){"1", "2", "3", "4", "5", "6", "7", "8", "9", "2147483648"};
|
||||
stack = fill_struct(ac, av);
|
||||
afficher_KO_OK(stack == NULL);
|
||||
}
|
||||
|
||||
void main_verif_double()
|
||||
{
|
||||
printf("verif_double: ");
|
||||
t_stack *new = ft_stacknew(0);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
t_stack *new3 = ft_stacknew(3);
|
||||
new2->next = new3;
|
||||
new->next = new2;
|
||||
afficher_KO_OK(verif_double(new2) == 1);
|
||||
afficher_KO_OK(verif_double(new) == 1);
|
||||
t_stack *new4 = ft_stacknew(0);
|
||||
new3->next = new4;
|
||||
afficher_KO_OK(is_sorted(new) == 0);
|
||||
new->nb_init = -1;
|
||||
afficher_KO_OK(is_sorted(new) == 0);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_replace_nb_init()
|
||||
{
|
||||
printf("verif_double: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
t_stack *new3 = ft_stacknew(20000);
|
||||
new2->next = new3;
|
||||
new->next = new2;
|
||||
replace_nb_init(new);
|
||||
afficher_KO_OK(new->nb == 1 && new->next->nb == 0 && new->next->next->nb == 2);
|
||||
t_stack *new4 = ft_stacknew(0);
|
||||
new3->next = new4;
|
||||
replace_nb_init(new);
|
||||
afficher_KO_OK(new->nb == 2 && new->next->nb == 1 && new->next->next->nb == 3 && new->next->next->next->nb == 0);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void main_is_sorted()
|
||||
{
|
||||
printf("stackis_sorted: ");
|
||||
t_stack *new = ft_stacknew(1000);
|
||||
new->nb = 1000;
|
||||
t_stack *new2 = ft_stacknew(1);
|
||||
new2->nb = 1;
|
||||
t_stack *new3 = ft_stacknew(3);
|
||||
new3->nb = 3;
|
||||
new2->next = new3;
|
||||
new->next = new2;
|
||||
afficher_KO_OK(is_sorted(new2) == 1);
|
||||
afficher_KO_OK(is_sorted(new) == 0);
|
||||
t_stack *new4 = ft_stacknew(2);
|
||||
new4->nb = 2;
|
||||
new3->next = new4;
|
||||
new->nb = 0;
|
||||
afficher_KO_OK(is_sorted(new) == 0);
|
||||
new4->nb = 6;
|
||||
afficher_KO_OK(is_sorted(new) == 1);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
main_stack();
|
||||
main_verif_nb();
|
||||
main_fill_struct();
|
||||
main_is_sorted();
|
||||
main_verif_double();
|
||||
main_replace_nb_init();
|
||||
}
|
Reference in New Issue
Block a user