data_structures
list_add_last.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* list_add_last.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 22:49:05 by unite #+# #+# */
10 /* Updated: 2020/09/07 22:02:31 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "list.h"
15 
16 void list_add_last(t_list *alst, const void *data)
17 {
18  t_link *link;
19 
20  link = ds_xcalloc(sizeof(t_link), 1);
21  link->content = alst->type->copy(data);
22  link->prev = alst->tail;
23  alst->size++;
24  alst->tail = link;
25  if (alst->size == 1)
26  alst->head = link;
27  else
28  alst->tail->prev->next = link;
29 }
s_type::copy
void *(* copy)(const void *)
A function pointer used to copy the data type.
Definition: types.h:50
list_add_last
void list_add_last(t_list *alst, const void *data)
Adds a copy of the specified element at the back of this list.
Definition: list_add_last.c:16
ds_xcalloc
void * ds_xcalloc(size_t count, size_t size)
Replicates behaviour of calloc from libc, but fails on memory allocation errors.
Definition: ds_xcalloc.c:22
s_list::head
t_link * head
The first link.
Definition: list.h:56
s_list
Doubly-linked list of generic items.
Definition: list.h:54
s_list::tail
t_link * tail
The last link.
Definition: list.h:57
s_list::size
size_t size
The number of items in the list.
Definition: list.h:58
s_list::type
const t_type * type
The type of items in this list.
Definition: list.h:59
list.h