btree new nodes now mallocs and memcpy

This commit is contained in:
Jack Halford 2016-11-13 23:59:19 +01:00
parent e635a1be88
commit ef60398100
3 changed files with 20 additions and 12 deletions

View file

@ -7,12 +7,13 @@ typedef struct s_btree t_btree;
struct s_btree struct s_btree
{ {
void *item;
size_t content_size;
struct s_btree *left; struct s_btree *left;
struct s_btree *right; struct s_btree *right;
void *item;
}; };
t_btree *btree_create_node(void *item); t_btree *btree_create_node(void const *item, size_t content_size);
void btree_insert_data( void btree_insert_data(
t_btree **root, t_btree **root,

View file

@ -12,13 +12,24 @@
#include "btree.h" #include "btree.h"
t_btree *btree_create_node(void *item) t_btree *btree_create_node(void const *item, size_t content_size)
{ {
t_btree *node; t_btree *node;
node = (t_btree *)malloc(sizeof(t_btree)); if (!(node = (t_btree *)malloc(sizeof(t_btree))))
return (NULL);
node->left = 0; node->left = 0;
node->right = 0; node->right = 0;
node->item = item;
return (node); return (node);
if (!content)
{
new->content_size = 0;
new->item = NULL;
}
else
{
new->content_size = content_size;
new->item = ft_memalloc(content_size + 1);
ft_memcpy(new->item, item, content_size);
}
} }

View file

@ -16,23 +16,19 @@ t_list *ft_lstnew(void const *content, size_t content_size)
{ {
t_list *new; t_list *new;
if (!(new = (t_list *)malloc(sizeof(*new))))
return (NULL);
new->next = NULL;
if (!content) if (!content)
{ {
new = malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = 0; new->content_size = 0;
new->content = NULL; new->content = NULL;
} }
else else
{ {
new = (t_list *)malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = content_size; new->content_size = content_size;
new->content = ft_memalloc(content_size + 1); new->content = ft_memalloc(content_size + 1);
ft_memcpy(new->content, content, content_size); ft_memcpy(new->content, content, content_size);
} }
new->next = NULL;
return (new); return (new);
} }