ft_lstadd_front

Subject

FT_LSTADD_FRONT (simplified)

NAME
    ft_lstadd_front -- Adds a new element at the front of the list
SYNOPSIS
    void ft_lstadd_front(t_list **lst, t_list *new);
DESCRIPTION
    Add the 'new' element at the front of the list
PARAMETERS
    lst: pointer address to the first element of the list
    new: pointer address of the new element to add to the list
RETURN VALUES
    None
AUTHORIZED EXTERNAL FUNCTIONS
    None

Understandable explanation

This function lets us add a new element to the front of an existing list.

We receive the new element and the existing list.

Hints

ft_lstadd_front
/* set the new element's next address to point 
 * to the start of the existing list
 */
/* set the existing list pointer to point to the new element
 */

Commented solution

ft_lstadd_front
ft_lstadd_front.c
#include "libft.h"

void ft_lstadd_front(t_list **alst, t_list *new)
{
    /* setting the new element's next address to point
     * to the start of the existing list
     */
    new->next = *alst;
    /* set the existing list pointer to point to the new element
     */
    *alst = new;
}

Last updated