Browse Source

Add bonus functions

master
Elouan MARTINET 4 years ago
parent
commit
0e4202e6ea
  1. 19
      ft_lstadd.c
  2. 29
      ft_lstdel.c
  3. 20
      ft_lstdelone.c
  4. 25
      ft_lstiter.c
  5. 28
      ft_lstlen.c
  6. 59
      ft_lstmap.c
  7. 28
      ft_lstncreate.c
  8. 36
      ft_lstnew.c
  9. 22
      ft_memdup.c

19
ft_lstadd.c

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:38:47 by emartine #+# #+# */
/* Updated: 2017/11/09 17:38:48 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd(t_list **alst, t_list *new)
{
new->next = *alst;
*alst = new;
}

29
ft_lstdel.c

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:38:34 by emartine #+# #+# */
/* Updated: 2017/11/09 17:38:35 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdel(t_list **alst, void (*del)(void *, size_t))
{
t_list *next;
t_list *current;
next = *alst;
while (next != NULL)
{
current = next;
del(current->content, current->content_size);
next = current->next;
free(current);
}
*alst = NULL;
}

20
ft_lstdelone.c

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:38:28 by emartine #+# #+# */
/* Updated: 2017/11/09 17:38:30 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t))
{
del((*alst)->content, (*alst)->content_size);
free(*alst);
*alst = NULL;
}

25
ft_lstiter.c

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:38:57 by emartine #+# #+# */
/* Updated: 2017/11/09 17:38:58 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(t_list *elem))
{
t_list *current;
current = lst;
while (current != NULL)
{
f(current);
current = current->next;
}
}

28
ft_lstlen.c

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 18:59:52 by emartine #+# #+# */
/* Updated: 2017/11/09 18:59:58 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_lstlen(const t_list *lst)
{
t_list *current;
size_t r;
current = (t_list *)lst;
r = 0;
while (current != NULL)
{
r++;
current = current->next;
}
return (r);
}

59
ft_lstmap.c

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:39:03 by emartine #+# #+# */
/* Updated: 2017/11/09 17:39:04 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Potentially unsafe depending to the real way to free the content.
*/
static t_list *ft_lstmap_del(t_list *r)
{
t_list *t;
while (r)
{
t = r;
r = r->next;
free(t->content);
free(t);
}
return (NULL);
}
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *r;
t_list *c;
t_list *t;
size_t len;
len = ft_lstlen(lst);
if (!(r = ft_lstncreate(len)))
return (NULL);
c = r;
while (len--)
{
t = f(lst);
if (t)
{
c->content = t->content;
c->content_size = t->content ? t->content_size : 0;
free(t);
}
else
return (ft_lstmap_del(r));
lst = lst->next;
r = r->next;
}
return (r);
}

28
ft_lstncreate.c

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstncreate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 20:40:53 by emartine #+# #+# */
/* Updated: 2017/11/09 20:40:55 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstncreate(size_t n)
{
t_list *first;
t_list *second;
first = NULL;
while (n--)
{
second = first;
first = ft_lstnew(NULL, 0);
first->next = second;
}
return (first);
}

36
ft_lstnew.c

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 17:38:16 by emartine #+# #+# */
/* Updated: 2017/11/09 17:38:19 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void const *content, size_t content_size)
{
t_list *r;
if (!(r = (t_list *)malloc(sizeof(*r))))
return (NULL);
if (content == NULL)
{
r->content = NULL;
r->content_size = 0;
}
else
{
if (!(r->content = ft_memdup(content, content_size)))
{
free(r);
return (NULL);
}
r->content_size = content_size;
}
return (r);
}

22
ft_memdup.c

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/09 18:29:37 by emartine #+# #+# */
/* Updated: 2017/11/09 18:29:39 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memdup(const void *src, size_t len)
{
char *r;
if (!(r = (char *)malloc(len)))
return (NULL);
return (ft_memcpy(r, src, len));
}
Loading…
Cancel
Save