ft_lstnew

Subject

FT_LSTNEW (simplified)

NAME
    ft_lstnew -- create a new list node element
SYNOPSIS
    t_list *ft_lstnew(void *content);
DESCRIPTION
    Allocate (with malloc(3)) and return the new element. The member variable 'content' is initialized with the value of the 'content' parameter. The 'next' variable is initialized to NULL.
PARAMETERS
    content: The content of the new element
RETURN VALUES
    The new element.
AUTHORIZED EXTERNAL FUNCTIONS
    malloc(3)

Understandable explanation

This function allocates memory for a new element of type t_list, setting its content to be the content parameter, and setting the next variable to NULL.

Then it returns the newly allocated / created element of the list.

Hints

ft_lstnew
/* declare a new list element */
/* allocate memory for it */
/* set the new element variables 
 * new->content = content
 * new->next = NULL
 */
/* return the new element */

Commented solution

ft_lstnew
ft_lstnew.c
#include "libft.h"

t_list *ft_lstnew(void *content)
{
    /* declaring the new list element
     */
    t_list *elem;
    
    /* allocating the memory for the new element
     */
    elem = malloc(sizeof(t_list));
    if (!elem)
        return (NULL);
    /* setting the content of the new element
     * to the 'content' parameter
     * and setting the 'next' to NULL
     */
    elem->content = content;
    elem->next = NULL;
    /* finally, we return the created element
     */
    return (elem);
}

Last updated