-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstsize_bonus.c
37 lines (35 loc) · 1.75 KB
/
ft_lstsize_bonus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmartin2 <lmartin2@student.42bcn.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/25 19:52:23 by lmartin2 #+# #+# */
/* Updated: 2023/02/03 19:11:17 by lmartin2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_bonus.h"
// The ft_lstsize function is implemented in the libft_bonus library. It
// calculates the number of elements in a linked list. This implementation
// allows for efficient calculation of the size of a linked list.
int ft_lstsize(t_list *lst)
// The function takes a pointer lst as input, which points to the head of the
// linked list.
{
int num;
// It initializes a variable num to 0, which will keep track of the number
// of elements in the list.
num = 0;
// A while loop is used to iterate through each element of the linked list.
// Inside the loop, the lst pointer is updated to point to the next element
// (lst->next) and the num variable is incremented by 1.
while (lst)
{
lst = lst->next;
num++;
}
// Once the end of the linked list is reached (i.e., lst becomes NULL),
// the loop exits and the final value of num is returned.
return (num);
}