98 lines
2 KiB
C
Executable file
98 lines
2 KiB
C
Executable file
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* utilities.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/02/13 12:08:14 by rparodi #+# #+# */
|
|
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../includes/push_swap.h"
|
|
|
|
int index_distance_head(t_list **stack, int index)
|
|
{
|
|
t_list *head;
|
|
int distance;
|
|
|
|
distance = 0;
|
|
head = *stack;
|
|
while (head)
|
|
{
|
|
if (head->index == index)
|
|
break ;
|
|
distance++;
|
|
head = head->next;
|
|
}
|
|
return (distance);
|
|
}
|
|
|
|
int get_min(t_list **stack, int val)
|
|
{
|
|
t_list *head;
|
|
int min;
|
|
|
|
head = *stack;
|
|
min = head->index;
|
|
while (head->next)
|
|
{
|
|
head = head->next;
|
|
if ((head->index < min) && head->index != val)
|
|
min = head->index;
|
|
}
|
|
return (min);
|
|
}
|
|
|
|
static t_list *get_next_min(t_list **stack)
|
|
{
|
|
t_list *head;
|
|
t_list *min;
|
|
int has_min;
|
|
|
|
min = NULL;
|
|
has_min = 0;
|
|
head = *stack;
|
|
if (head)
|
|
{
|
|
while (head)
|
|
{
|
|
if ((head->index == -1) && (!has_min || head->value < min->value))
|
|
{
|
|
min = head;
|
|
has_min = 1;
|
|
}
|
|
head = head->next;
|
|
}
|
|
}
|
|
return (min);
|
|
}
|
|
|
|
void index_stack(t_list **stack)
|
|
{
|
|
t_list *head;
|
|
int index;
|
|
|
|
index = 0;
|
|
head = get_next_min(stack);
|
|
while (head)
|
|
{
|
|
head->index = index++;
|
|
head = get_next_min(stack);
|
|
}
|
|
}
|
|
|
|
int is_sorted(t_list **stack)
|
|
{
|
|
t_list *head;
|
|
|
|
head = *stack;
|
|
while (head && head->next)
|
|
{
|
|
if (head->value > head->next->value)
|
|
return (0);
|
|
head = head->next;
|
|
}
|
|
return (1);
|
|
}
|