ft_lstsize

Subject

FT_LSTSIZE (simplified)

NAME
    ft_lstsize -- returns the number of element in the list
SYNOPSIS
    int *ft_lstsize(t_list *lst);
DESCRIPTION
    Count the number of elements of the list
PARAMETERS
    lst: start of the list
RETURN VALUES
    The size of the list
AUTHORIZED EXTERNAL FUNCTIONS
    None

Understandable explanation

I think the subject is clear on what this function does, we have to return the number of element of the list.

Hints

/* loop over the list */
/* return the count */

Commented solution

ft_lstsize
ft_lstsize.c
#include "libft.h"

int ft_lstsize(t_list *lst)
{
    /* I used a tmp variable so that we don't modify the 
     * existing list
     */
    t_list *tmp;
    int i;
    
    tmp = lst;
    i = 0;
    /* we loop as long as tmp is not equal to null
     * since the last element's next point to null
     * we will be iterating over all the elements of the list
     */
    while (tmp)
    {
        /* set the tmp to be its 'next' element */
        tmp = tmp->next;
        i++;
    }
    /* returning the count */
    return (i);
}

Last updated