Last active
September 4, 2016 10:55
-
-
Save lucasscariot/a110c860546d3dae5a44a3dfbcdc8b6b to your computer and use it in GitHub Desktop.
Chained List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* **************************************************************** */ | |
/* 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