Skip to content

Instantly share code, notes, and snippets.

@lucasscariot
Last active September 4, 2016 10:55
Show Gist options
  • Save lucasscariot/a110c860546d3dae5a44a3dfbcdc8b6b to your computer and use it in GitHub Desktop.
Save lucasscariot/a110c860546d3dae5a44a3dfbcdc8b6b to your computer and use it in GitHub Desktop.
Chained List
/* **************************************************************** */
/* Create/Insert chained list */
/* (With Next&Prev) */
/* 1_ Insert in the front */
/* 2_ Insert in the back */
/* */
/* **************************************************************** */
t_list *ft_insert_front(t_list *list, char *content)
{
t_list *new;
t_list *tmp;
new = (t_list*)malloc(sizeof(*new));
new->content = ft_strdup(content);
new->next = NULL;
new->prev = NULL;
if (!list)
return (new);
tmp = list;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
new->prev = tmp;
return (list);
}
t_list *ft_insert_back(t_list *list, char *content)
{
t_list *new;
t_list *tmp;
new = (t_list*)malloc(sizeof(*new));
new->content = ft_strdup(content);
new->next = NULL;
new->prev = NULL;
if (!list)
return (new);
tmp = list;
while (tmp->prev)
tmp = tmp->prev;
tmp->prev = new;
new->next = tmp;
return (new);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment