Files
42-Push_Swap/stack_utils/stack_add.c

55 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/08 15:52:40 by dgaillet #+# #+# */
/* Updated: 2025/12/08 16:03:54 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
t_stack *new_stack(int value)
{
t_stack *new;
new = malloc(sizeof(t_stack));
if (!new)
return (NULL);
new->value = value;
new->next = NULL;
new->previous = NULL;
return (new);
}
void stack_add_back(t_stack **stack, t_stack *new)
{
if (!stack || !new)
return ;
if (!(*stack))
{
(*stack) = new;
new->next = new;
new->previous = new;
}
else
{
(*stack)->previous->next = new;
new->previous = *(stack)->previous;
(*stack)->previous = new;
new->next = *stack;
}
}
void stack_add_front(t_stack **stack, t_stack *new)
{
if (!stack || *new)
return ;
stack_add_back(stack, new);
*stack = new;
}