intra to gitea
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* calc_cheap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/03 11:32:29 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:05:31 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int calc_move(t_stack *stack_a, t_stack *stack_b, int index_b)
|
||||
{
|
||||
int index_a;
|
||||
int value_b;
|
||||
|
||||
value_b = return_value(stack_b, index_b);
|
||||
index_a = calc_index_a(stack_a, value_b);
|
||||
return (calc_move_stack(stack_b, index_b) \
|
||||
+ calc_move_stack(stack_a, index_a));
|
||||
}
|
||||
|
||||
int calc_cheaper(t_stack *stack_a, t_stack *stack_b)
|
||||
{
|
||||
int index;
|
||||
int cheaper;
|
||||
int cheaper_index;
|
||||
t_node *curr;
|
||||
|
||||
curr = stack_b->top;
|
||||
index = 1;
|
||||
cheaper_index = 1;
|
||||
cheaper = calc_move(stack_a, stack_b, index);
|
||||
index++;
|
||||
while (curr->next)
|
||||
{
|
||||
if (cheaper > calc_move(stack_a, stack_b, index))
|
||||
{
|
||||
cheaper = calc_move(stack_a, stack_b, index);
|
||||
cheaper_index = index;
|
||||
}
|
||||
index++;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (cheaper_index);
|
||||
}
|
||||
|
||||
int calc_move_stack(t_stack *stack, int index)
|
||||
{
|
||||
if (index <= stack->size / 2)
|
||||
return (index - 1);
|
||||
return (stack->size - index + 1);
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* calc_cheap_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/02 01:37:25 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:07:37 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int calc_index_min(t_stack *stack_a)
|
||||
{
|
||||
t_node *curr;
|
||||
int min;
|
||||
|
||||
curr = stack_a->top;
|
||||
min = curr->value;
|
||||
curr = curr->next;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value < min)
|
||||
min = curr->value;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (min);
|
||||
}
|
||||
|
||||
int calc_index_max(t_stack *stack_a)
|
||||
{
|
||||
t_node *curr;
|
||||
int max;
|
||||
|
||||
curr = stack_a->top;
|
||||
max = curr->value;
|
||||
curr = curr->next;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value > max)
|
||||
max = curr->value;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (max);
|
||||
}
|
||||
|
||||
int return_index(t_stack *stack, int value)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
curr = stack->top;
|
||||
while (curr)
|
||||
{
|
||||
if (curr->value == value)
|
||||
return (i);
|
||||
i++;
|
||||
curr = curr->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int calc_index_a(t_stack *stack_a, int value)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
|
||||
i = 2;
|
||||
curr = stack_a->top;
|
||||
if (value > calc_index_max(stack_a) || value < calc_index_min(stack_a))
|
||||
return (return_index(stack_a, calc_index_min(stack_a)));
|
||||
while (curr && curr->next)
|
||||
{
|
||||
if (curr->value < value && curr->next->value > value)
|
||||
return (i);
|
||||
i++;
|
||||
curr = curr->next;
|
||||
}
|
||||
if (curr->value < value && stack_a->top->value > value)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int return_value(t_stack *stack, int index)
|
||||
{
|
||||
t_node *curr;
|
||||
|
||||
if (!stack || !stack->top || index <= 0)
|
||||
return (0);
|
||||
if (index > stack->size)
|
||||
return (0);
|
||||
curr = stack->top;
|
||||
while (index > 1 && curr)
|
||||
{
|
||||
if (!curr->next)
|
||||
return (0);
|
||||
curr = curr->next;
|
||||
index--;
|
||||
}
|
||||
return (curr->value);
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:13:52 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 19:40:43 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int fill_stack(t_stack *stack_a, char **av)
|
||||
{
|
||||
int i;
|
||||
int num;
|
||||
|
||||
i = 0;
|
||||
while (av[i])
|
||||
{
|
||||
if (ft_atoll(av[i]) > 2147483647)
|
||||
return (ft_printf_fd(2, "Error\n"), 0);
|
||||
num = ft_atoll(av[i]);
|
||||
if (valid_input(av[i]) == 0 && num <= INT_MAX && num >= INT_MIN \
|
||||
&& in_stack(stack_a, (int)num) == 0)
|
||||
{
|
||||
add_stack(stack_a, (int)num);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_stack *stack_a;
|
||||
t_stack *stack_b;
|
||||
char **av_pars;
|
||||
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
stack_a = init_stack();
|
||||
stack_b = init_stack();
|
||||
if (!stack_a || !stack_b)
|
||||
return (1);
|
||||
av_pars = pars(av);
|
||||
if (!av_pars)
|
||||
return (ft_printf_fd(1, "error\n"), 1);
|
||||
if (!fill_stack(stack_a, av_pars))
|
||||
return (free_stack(stack_a), free_stack(stack_b), \
|
||||
free_array(av_pars), 1);
|
||||
if (is_sorted(stack_a) == 1)
|
||||
return (free_stack(stack_a), free_stack(stack_b), \
|
||||
free_array(av_pars), 1);
|
||||
sort(stack_a, stack_b);
|
||||
return (free_stack(stack_a), free_stack(stack_b), free_array(av_pars), 0);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* op.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:20:06 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:48:32 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void add_stack(t_stack *stack, int value)
|
||||
{
|
||||
t_node *new_node;
|
||||
t_node *current;
|
||||
|
||||
new_node = malloc(sizeof(t_node));
|
||||
if (!new_node)
|
||||
return ;
|
||||
new_node->value = value;
|
||||
new_node->next = NULL;
|
||||
if (stack->top == NULL)
|
||||
{
|
||||
stack->top = new_node;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = stack->top;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
}
|
||||
current->next = new_node;
|
||||
}
|
||||
stack->size++;
|
||||
}
|
||||
|
||||
int swap(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *second;
|
||||
|
||||
if (stack->top == NULL || stack->top->next == NULL)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
second = first->next;
|
||||
first->next = second->next;
|
||||
second->next = first;
|
||||
stack->top = second;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int push(t_stack *stack_1, t_stack *stack_2)
|
||||
{
|
||||
t_node *first_1;
|
||||
t_node *first_2;
|
||||
t_node *tmp;
|
||||
|
||||
if (stack_1->top == NULL)
|
||||
return (0);
|
||||
first_1 = stack_1->top;
|
||||
first_2 = stack_2->top;
|
||||
tmp = first_1->next;
|
||||
first_1->next = first_2;
|
||||
stack_1->top = tmp;
|
||||
stack_2->top = first_1;
|
||||
stack_2->size++;
|
||||
stack_1->size--;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int rrotate(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *last;
|
||||
t_node *before_last;
|
||||
|
||||
if (stack->size < 2)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
before_last = stack->top;
|
||||
last = go_to(stack->top, stack->size);
|
||||
before_last = go_to(stack->top, (stack->size - 1));
|
||||
if (!last || !before_last)
|
||||
return (-1);
|
||||
before_last->next = NULL;
|
||||
last->next = first;
|
||||
stack->top = last;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int rotate(t_stack *stack)
|
||||
{
|
||||
t_node *first;
|
||||
t_node *last;
|
||||
t_node *after_first;
|
||||
|
||||
if (stack->size < 2)
|
||||
return (0);
|
||||
first = stack->top;
|
||||
last = stack->top;
|
||||
last = go_to(stack->top, stack->size);
|
||||
if (!last)
|
||||
return (-1);
|
||||
after_first = first->next;
|
||||
last->next = first;
|
||||
first->next = NULL;
|
||||
stack->top = after_first;
|
||||
return (1);
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pars.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/05 08:26:16 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/05 08:26:16 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
char *init_empty_str(void)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = malloc(sizeof(char));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
str[0] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
void free_array(char **array)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (!array)
|
||||
return ;
|
||||
i = 0;
|
||||
while (array[i])
|
||||
{
|
||||
if (array[i])
|
||||
free(array[i]);
|
||||
i++;
|
||||
}
|
||||
free(array);
|
||||
}
|
||||
|
||||
char **pars(char **av)
|
||||
{
|
||||
char *str;
|
||||
char **av_pars;
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
str = init_empty_str();
|
||||
if (!str)
|
||||
return (printf("error\n"), NULL);
|
||||
while (av[i])
|
||||
{
|
||||
str = ft_strcat(str, av[i]);
|
||||
if (!str)
|
||||
return (printf("error\n"), NULL);
|
||||
i++;
|
||||
}
|
||||
av_pars = ft_split(str, ' ');
|
||||
free(str);
|
||||
if (!av_pars)
|
||||
{
|
||||
printf("error\n");
|
||||
return (NULL);
|
||||
}
|
||||
return (av_pars);
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 10:00:34 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:13:21 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void sort(t_stack *stack_a, t_stack *stack_b)
|
||||
{
|
||||
int median;
|
||||
t_node *current;
|
||||
|
||||
current = stack_a->top;
|
||||
median = get_median(stack_a, stack_a->size);
|
||||
if (stack_a->size == 2)
|
||||
sort_2(stack_a);
|
||||
else
|
||||
{
|
||||
while (current->next->next->next)
|
||||
{
|
||||
push(stack_a, stack_b);
|
||||
ft_printf_fd(1, "pb\n");
|
||||
if (current->value > median)
|
||||
{
|
||||
rotate(stack_b);
|
||||
ft_printf_fd(1, "rb\n");
|
||||
}
|
||||
current = stack_a->top;
|
||||
}
|
||||
sort_3(stack_a);
|
||||
while (stack_b->size != 0)
|
||||
make_move(stack_a, stack_b, calc_cheaper(stack_a, stack_b));
|
||||
final_sort(stack_a);
|
||||
}
|
||||
}
|
||||
|
||||
void final_sort(t_stack *stack_a)
|
||||
{
|
||||
int index_min;
|
||||
|
||||
index_min = return_index(stack_a, calc_index_min(stack_a));
|
||||
make_move_a(stack_a, calc_move_stack_2(stack_a, index_min));
|
||||
}
|
||||
|
||||
void opti(int *move)
|
||||
{
|
||||
if (move[0] > 0 && move[1] > 0)
|
||||
{
|
||||
if (move[0] > move[1])
|
||||
{
|
||||
move[2] = move[1];
|
||||
move[0] = move[0] - move[1];
|
||||
move[1] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
move[2] = move[0];
|
||||
move[1] = move[1] - move[0];
|
||||
move[0] = 0;
|
||||
}
|
||||
}
|
||||
else if (move[0] < 0 && move[1] < 0)
|
||||
opti_2(move);
|
||||
}
|
||||
|
||||
void opti_2(int *move)
|
||||
{
|
||||
if (move[0] < move[1])
|
||||
{
|
||||
move[2] = move[0];
|
||||
move[1] = move[1] - move[0];
|
||||
move[0] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
move[2] = move[1];
|
||||
move[0] = move[0] - move[1];
|
||||
move[1] = 0;
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_2.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/03 11:40:53 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:10:08 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void make_move(t_stack *stack_a, t_stack *stack_b, int cheap_ind)
|
||||
{
|
||||
int move[3];
|
||||
int value_b;
|
||||
int index_a;
|
||||
|
||||
move[1] = calc_move_stack_2(stack_b, cheap_ind);
|
||||
value_b = return_value(stack_b, cheap_ind);
|
||||
index_a = calc_index_a(stack_a, value_b);
|
||||
move[0] = calc_move_stack_2(stack_a, index_a);
|
||||
move[2] = 0;
|
||||
opti(move);
|
||||
make_move_b(stack_b, move[1]);
|
||||
make_move_a(stack_a, move[0]);
|
||||
make_move_r(stack_b, stack_a, move[2]);
|
||||
push(stack_b, stack_a);
|
||||
ft_printf_fd(1, "pa\n");
|
||||
}
|
||||
|
||||
void make_move_a(t_stack *stack_a, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rra\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make_move_b(t_stack *stack_b, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_b);
|
||||
ft_printf_fd(1, "rb\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_b);
|
||||
ft_printf_fd(1, "rrb\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void make_move_r(t_stack *stack_b, t_stack *stack_a, int move)
|
||||
{
|
||||
if (move > 0)
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rotate(stack_b);
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "rr\n");
|
||||
move--;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (move != 0)
|
||||
{
|
||||
rrotate(stack_b);
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rrr\n");
|
||||
move++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int calc_move_stack_2(t_stack *stack, int index)
|
||||
{
|
||||
t_node *curr;
|
||||
int i;
|
||||
int ind;
|
||||
|
||||
ind = index;
|
||||
curr = stack->top;
|
||||
while (ind > 1)
|
||||
{
|
||||
curr = curr->next;
|
||||
ind--;
|
||||
}
|
||||
if (index - 1 < stack->size - index + 1)
|
||||
i = index - 1;
|
||||
else
|
||||
i = (stack->size - index + 1) * -1;
|
||||
return (i);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_tiny.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/21 10:30:01 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:58:41 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
void sort_2(t_stack *stack_a)
|
||||
{
|
||||
t_node *value1;
|
||||
t_node *value2;
|
||||
|
||||
value1 = stack_a->top;
|
||||
value2 = value1->next;
|
||||
if (value1->value >= value2->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
}
|
||||
}
|
||||
|
||||
void sort_3(t_stack *stack_a)
|
||||
{
|
||||
t_node *v1;
|
||||
t_node *v2;
|
||||
t_node *v3;
|
||||
|
||||
v1 = stack_a->top;
|
||||
v2 = v1->next;
|
||||
v3 = v2->next;
|
||||
if (v2->value < v1->value && v2->value > v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
swap(stack_a);
|
||||
ft_printf_fd(1, "ra\nsa\n");
|
||||
}
|
||||
if (v2->value < v1->value && v2->value < v3->value && v1->value < v3->value)
|
||||
{
|
||||
ft_printf_fd(1, "sa\n");
|
||||
swap(stack_a);
|
||||
}
|
||||
if (v2->value < v1->value && v2->value < v3->value && v1->value > v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
ft_printf_fd(1, "ra\n");
|
||||
}
|
||||
sort_3_bis(v1, v2, v3, stack_a);
|
||||
}
|
||||
|
||||
void sort_3_bis(t_node *v1, t_node *v2, t_node *v3, t_stack *stack_a)
|
||||
{
|
||||
if (v2->value > v1->value && v2->value > v3->value && v1->value < v3->value)
|
||||
{
|
||||
rotate(stack_a);
|
||||
swap(stack_a);
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "ra\nsa\nrra\n");
|
||||
}
|
||||
if (v2->value > v1->value && v2->value > v3->value && v1->value > v3->value)
|
||||
{
|
||||
rrotate(stack_a);
|
||||
ft_printf_fd(1, "rra\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 08:33:48 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:57:30 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int get_median(t_stack *stack, int size)
|
||||
{
|
||||
int *table;
|
||||
int i;
|
||||
t_node *current;
|
||||
int median;
|
||||
|
||||
if (!size || !stack || !stack->top)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
table = malloc(size * sizeof(int));
|
||||
if (!table)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
i = 0;
|
||||
current = stack->top;
|
||||
while (current && i < size)
|
||||
{
|
||||
table[i++] = current->value;
|
||||
current = current->next;
|
||||
}
|
||||
sort_int_table(table, size);
|
||||
median = median_calc(table, size);
|
||||
return (free(table), median);
|
||||
}
|
||||
|
||||
void sort_int_table(int *tab, int size)
|
||||
{
|
||||
int a;
|
||||
int tmp;
|
||||
|
||||
a = 1;
|
||||
if (size > 1)
|
||||
{
|
||||
while (a < size)
|
||||
{
|
||||
if (tab[a] < tab[a - 1])
|
||||
{
|
||||
tmp = tab[a];
|
||||
tab[a] = tab[a - 1];
|
||||
tab[a - 1] = tmp;
|
||||
a = 0;
|
||||
}
|
||||
++a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int median_calc(int *tab, int size)
|
||||
{
|
||||
if (size % 2 == 0)
|
||||
return (tab[size / 2 -1]);
|
||||
else
|
||||
return (tab[(size - 1) / 2 + 1]);
|
||||
}
|
||||
|
||||
int find_index_between(t_stack *stack_a, int value)
|
||||
{
|
||||
t_node *a_cur;
|
||||
int index;
|
||||
int found;
|
||||
|
||||
a_cur = stack_a->top;
|
||||
index = 1;
|
||||
found = 0;
|
||||
while (a_cur && a_cur->next && !found)
|
||||
{
|
||||
if (a_cur->value < value && a_cur->next->value > value)
|
||||
found = 1;
|
||||
else
|
||||
{
|
||||
index++;
|
||||
a_cur = a_cur->next;
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
return (index);
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/12 19:00:13 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 18:08:42 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
t_stack *init_stack(void)
|
||||
{
|
||||
t_stack *stack;
|
||||
|
||||
stack = malloc(sizeof(t_stack));
|
||||
if (!stack)
|
||||
{
|
||||
return (NULL);
|
||||
ft_printf_fd(2, "Error");
|
||||
}
|
||||
stack->top = NULL;
|
||||
stack->size = 0;
|
||||
return (stack);
|
||||
}
|
||||
|
||||
void free_stack(t_stack *stack)
|
||||
{
|
||||
t_node *current;
|
||||
t_node *next;
|
||||
|
||||
current = stack->top;
|
||||
while (current)
|
||||
{
|
||||
next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
free(stack);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 00:09:54 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:51:14 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
t_node *go_to(t_node *top, int pos)
|
||||
{
|
||||
t_node *node;
|
||||
int i;
|
||||
|
||||
if (pos < 1)
|
||||
return (top);
|
||||
node = top;
|
||||
i = 1;
|
||||
while (i < pos)
|
||||
{
|
||||
node = node->next;
|
||||
i++;
|
||||
}
|
||||
return (node);
|
||||
}
|
||||
|
||||
int valid_input(const char *str)
|
||||
{
|
||||
int i;
|
||||
int sign_count;
|
||||
long long num;
|
||||
|
||||
i = 0;
|
||||
sign_count = 0;
|
||||
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
|
||||
i++;
|
||||
while (str[i] == '+' || str[i] == '-')
|
||||
{
|
||||
sign_count++;
|
||||
i++;
|
||||
}
|
||||
if (sign_count > 1 || str[i] == '\0')
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] < '0' || str[i] > '9')
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
i++;
|
||||
}
|
||||
num = ft_atoll(str);
|
||||
if (num > INT_MAX || num < INT_MIN)
|
||||
return (ft_printf_fd(2, "Error\n"), 1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int in_stack(t_stack *stack, int n)
|
||||
{
|
||||
t_node *current;
|
||||
|
||||
current = stack->top;
|
||||
while (current)
|
||||
{
|
||||
if (current->value == n)
|
||||
{
|
||||
ft_printf_fd(2, "Error\n");
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_sorted(t_stack *stack)
|
||||
{
|
||||
t_node *current;
|
||||
|
||||
if (!stack || !stack->top)
|
||||
return (1);
|
||||
current = stack->top;
|
||||
while (current->next)
|
||||
{
|
||||
if (current->value > current->next->value)
|
||||
return (0);
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
Reference in New Issue
Block a user