96 lines
2.2 KiB
C
96 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ps_stktools.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/02/22 14:41:27 by tmaze #+# #+# */
|
|
/* Updated: 2019/03/02 15:23:43 by tmaze ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
t_stack *ps_stknew(int nb)
|
|
{
|
|
t_stack *ret;
|
|
|
|
if ((ret = (t_stack*)ft_memalloc(sizeof(t_stack))) != NULL)
|
|
{
|
|
ret->nb = nb;
|
|
ret->next = NULL;
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
void ps_stkpsh(t_psdata *data, char c, t_stack *new)
|
|
{
|
|
t_stack *ind;
|
|
t_stack **s;
|
|
size_t *size;
|
|
|
|
s = NULL;
|
|
if (data != NULL && new != NULL)
|
|
{
|
|
if (c == 'a')
|
|
s = &(data->a);
|
|
if (c == 'a')
|
|
size = &(data->size_a);
|
|
if (c == 'b')
|
|
s = &(data->b);
|
|
if (c == 'b')
|
|
size = &(data->size_b);
|
|
if (s != NULL)
|
|
{
|
|
ind = *s;
|
|
*s = new;
|
|
new->next = ind;
|
|
(*size)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
t_stack *ps_stkpop(t_psdata *data, char c)
|
|
{
|
|
t_stack *ret;
|
|
t_stack **s;
|
|
size_t *size;
|
|
|
|
if (c == 'a')
|
|
s = &(data->a);
|
|
if (c == 'a')
|
|
size = &(data->size_a);
|
|
if (c == 'b')
|
|
s = &(data->b);
|
|
if (c == 'b')
|
|
size = &(data->size_b);
|
|
else if (c != 'a' && c != 'b')
|
|
return (NULL);
|
|
ret = NULL;
|
|
if (s != NULL && *s != NULL)
|
|
{
|
|
ret = *s;
|
|
*s = ret->next;
|
|
(*size)--;
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
void ft_lstdelstr(void *content, size_t content_size)
|
|
{
|
|
ft_strdel((char**)&content);
|
|
content_size = 0;
|
|
}
|
|
|
|
void ps_stkclean(t_psdata *data)
|
|
{
|
|
t_stack *tmp;
|
|
|
|
while ((tmp = ps_stkpop(data, 'a')) != NULL)
|
|
ft_memdel((void**)&tmp);
|
|
while ((tmp = ps_stkpop(data, 'b')) != NULL)
|
|
ft_memdel((void**)&tmp);
|
|
ft_lstdel(&(data->op), &ft_lstdelstr);
|
|
}
|