pushswap/src/finale.c

104 lines
2.1 KiB
C
Raw Normal View History

2023-03-01 04:36:58 +01:00
/* ************************************************************************** */
/* */
/* .--. _ */
/* finale.c |o_o || | */
/* |:_/ || |_ _ ___ __ */
/* By: djonker <djonker@student.codam.nl> // \ \ __| | | \ \/ / */
/* (| | )|_| |_| |> < */
/* Created: 2022/11/22 19:34:19 by djonker /'\_ _/`\__|\__,_/_/\_\ */
/* Updated: 2022/11/24 03:54:09 by djonker \___)=(___/ */
/* */
/* ************************************************************************** */
#include "../push_swap.h"
int upordownfinal(t_varlist *vl)
{
t_stack *fstack;
t_stack *rstack;
fstack = vl->a;
rstack = vl->a;
vl->direction = 3;
if (vl->a->id == 1)
vl->direction = 1;
while (vl->direction == 3)
{
fstack = fstack->n;
if (fstack->id == 1)
vl->direction = 1;
rstack = rstack->p;
if (rstack->id == 1)
vl->direction = 2;
}
return (0);
}
int lowontop(t_varlist *vl)
{
upordownfinal(vl);
if (vl->direction == 1)
while (1 != vl->a->id)
ra(vl, 1);
if (vl->direction == 2)
while (1 != vl->a->id)
rra(vl, 1);
return (0);
}
int freestacka(t_varlist *vl)
{
t_stack *cstack;
t_stack *dstack;
while (vl->a)
{
cstack = vl->a;
dstack = vl->a;
if (cstack == cstack->n)
vl->a = NULL;
else
{
cstack = vl->a->p;
vl->a = vl->a->n;
cstack->n = vl->a;
vl->a->p = cstack;
}
free (dstack);
}
return (0);
}
int freestackb(t_varlist *vl)
{
t_stack *cstack;
t_stack *dstack;
while (vl->b)
{
cstack = vl->b;
dstack = vl->b;
if (cstack == cstack->n)
vl->b = NULL;
else
{
cstack = vl->b->p;
vl->b = vl->b->n;
cstack->n = vl->b;
vl->b->p = cstack;
}
free (dstack);
}
return (0);
}
int freeall(t_varlist *vl)
{
if (vl->a)
freestacka(vl);
if (vl->b)
freestackb(vl);
free (vl);
return (0);
}