aboutsummaryrefslogtreecommitdiff
path: root/list.c
blob: 12c6e62c68a71907f9d06564e3156c0b20bdd742 (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
#include <stdlib.h>
#include "header.h"

t_list	*list_new(t_pformat *data)
{
	t_list	*list;

	if ((list = (t_list*)malloc(sizeof(t_list))) == NULL)
		return NULL;
	list->data = data;
	list->next = NULL;
	return (list);
}

t_list	*list_destroy(t_list *list)
{
	while (list != NULL)
		list_pop_front(&list);
	return (NULL);
}


void	list_push_front(t_list **list, t_list *new_front)
{
	new_front->next = *list;
	*list = new_front;
}

void	list_push_back(t_list **list, t_list *new_back)
{
	t_list	*cursor;

	if (*list == NULL)
	{
		*list = new_back;
		return ;
	}
	cursor = *list;
	while (cursor->next != NULL)
		cursor = cursor->next;
	cursor->next = new_back;
}

void	list_pop_front(t_list **list)
{
	t_list	*tmp;

	if (*list == NULL)
		return ;
	tmp = (*list)->next;
	free((*list)->data);
	free(*list);
	*list = tmp;
}