Push Swap
This commit is contained in:
74
stack.c
Normal file
74
stack.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* stack.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/12 06:02:23 by aortigos #+# #+# */
|
||||
/* Updated: 2024/08/12 06:22:20 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
t_stack *stack_new(int value)
|
||||
{
|
||||
t_stack *new;
|
||||
|
||||
new = malloc(sizeof(t_stack));
|
||||
if (!new)
|
||||
return (NULL);
|
||||
new->value = value;
|
||||
new->index = 0;
|
||||
new->pos = -1;
|
||||
new->target = -1;
|
||||
new->cost_a = -1;
|
||||
new->cost_b = -1;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
void stack_add(t_stack **stack, t_stack *new)
|
||||
{
|
||||
t_stack *bottom;
|
||||
|
||||
if (!new)
|
||||
return ;
|
||||
if (!*stack)
|
||||
{
|
||||
*stack = new;
|
||||
return ;
|
||||
}
|
||||
bottom = get_bottom(*stack);
|
||||
bottom->next = new;
|
||||
}
|
||||
|
||||
t_stack *get_bottom(t_stack *stack)
|
||||
{
|
||||
while (stack && stack->next != NULL)
|
||||
stack = stack->next;
|
||||
return (stack);
|
||||
}
|
||||
|
||||
t_stack *before_bottom(t_stack *stack)
|
||||
{
|
||||
while (stack && stack->next->next != NULL)
|
||||
stack = stack->next;
|
||||
return (stack);
|
||||
}
|
||||
|
||||
int get_stack_size(t_stack *stack)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 0;
|
||||
if (!stack)
|
||||
return (0);
|
||||
while (stack)
|
||||
{
|
||||
stack = stack->next;
|
||||
size++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
Reference in New Issue
Block a user