added dual linked list

This commit is contained in:
Tanguy MAZE 2018-09-30 17:42:57 +02:00
parent 6dcecc0844
commit 4d2356e551
6 changed files with 135 additions and 1 deletions

21
ft_lst2add.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/30 17:07:48 by tmaze #+# #+# */
/* Updated: 2018/09/30 17:15:59 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lst2add(t_list2 **alst, t_list2 *new)
{
new->next = *alst->next;
new->prev = *alst;
new->next->prev = new;
*alst->next = new;
}

20
ft_lst2del.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/30 17:32:37 by tmaze #+# #+# */
/* Updated: 2018/09/30 17:39:27 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lst2del(t_list2 **alst, void (*del)(void*, size_t))
{
if ((*alst)->next != NULL)
ft_lst2del(&((*alst)->next), del);
ft_lst2delone(alst, del);
}

20
ft_lst2delone.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2delone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/30 17:19:23 by tmaze #+# #+# */
/* Updated: 2018/09/30 17:25:38 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lst2delone(t_list2 **alst, void (*del)(void*, size_t))
{
(*del)((*alst)->content, (*alst)->content_size);
free(*alst);
*alst = NULL;
}

40
ft_lst2new.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/30 17:28:23 by tmaze #+# #+# */
/* Updated: 2018/09/30 17:31:46 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list2 *ft_lstnew(void const *content, size_t content_size)
{
t_list2 *ret;
if ((ret = (t_list*)malloc(sizeof(t_list2))) != NULL)
{
if (!content)
{
ret->content = NULL;
ret->content_size = 0;
}
else
{
if ((ret->content = malloc(content_size)) == NULL)
{
free(ret);
return (0);
}
ft_memmove(ret->content, content, content_size);
ret->content_size = content_size;
}
ret->next = NULL;
ret->prev = NULL;
}
return (ret);
}

21
ft_lst2size.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/09/30 17:38:55 by tmaze #+# #+# */
/* Updated: 2018/09/30 17:39:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_lst2size(t_list2 *lst)
{
if (lst == NULL)
return (0);
else
return (ft_lst2size(lst->next) + 1);
}

14
libft.h
View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */ /* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
/* Updated: 2018/06/07 16:54:31 by tmaze ### ########.fr */ /* Updated: 2018/09/30 17:04:54 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,6 +28,18 @@ typedef struct s_list
struct s_list *next; struct s_list *next;
} t_list; } t_list;
/*
** définition type t_list2
*/
typedef struct s_list2
{
struct s_list2 *prev;
void *content;
size_t content_size;
struct s_list2 *next;
} t_list2;
/* /*
** definition macro BUFF_SIZE ** definition macro BUFF_SIZE
*/ */