aboutsummaryrefslogtreecommitdiff
path: root/ft_printf/list.c
blob: 99491f4de1aacbb863d861a57d4c0ffb93bb3f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   list.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/10/29 00:14:50 by cacharle          #+#    #+#             */
/*   Updated: 2019/11/05 23:45:42 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_vasprintf.h"

t_flist	*list_new(t_pformat *content)
{
	t_flist	*lst;

	if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL)
		return (NULL);
	lst->content = content;
	lst->next = NULL;
	return (lst);
}

void	*list_destroy(t_flist **lst)
{
	if (lst == NULL)
		return (NULL);
	while (*lst != NULL)
		list_pop_front(lst);
	return (NULL);
}

void	list_push_front(t_flist **lst, t_flist *new)
{
	if (lst == NULL || new == NULL)
		return ;
	new->next = *lst;
	*lst = new;
}

void	list_pop_front(t_flist **lst)
{
	t_flist	*tmp;

	if (lst == NULL || *lst == NULL)
		return ;
	tmp = (*lst)->next;
	free((*lst)->content);
	free(*lst);
	*lst = tmp;
}

t_flist	*list_reverse(t_flist *lst)
{
	t_flist	*tmp;

	if (lst == NULL)
		return (NULL);
	if (lst->next == NULL)
		return (lst);
	tmp = list_reverse(lst->next);
	lst->next->next = lst;
	lst->next = NULL;
	return (tmp);
}