Push the final version
This commit is contained in:
commit
d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions
98
sources/utilities.c
Executable file
98
sources/utilities.c
Executable file
|
|
@ -0,0 +1,98 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue