2023-03-01 04:36:58 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
2023-10-26 19:09:02 +02:00
|
|
|
/* :::::::: */
|
|
|
|
/* finale.c :+: :+: */
|
|
|
|
/* +:+ */
|
|
|
|
/* By: houtworm <codam@houtworm.net> +#+ */
|
|
|
|
/* +#+ */
|
|
|
|
/* Created: 2023/10/26 19:03:25 by houtworm #+# #+# */
|
|
|
|
/* Updated: 2023/10/26 19:03:25 by houtworm ######## odam.nl */
|
2023-03-01 04:36:58 +01:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|