File tree Expand file tree Collapse file tree 5 files changed +55
-8
lines changed Expand file tree Collapse file tree 5 files changed +55
-8
lines changed Original file line number Diff line number Diff line change
1
+ #include "lists.h"
2
+
3
+ /**
4
+ * list_len_recursion - A function that gets the number of nodes of linked list
5
+ * @h: head of linked list
6
+ * Return: number of nodes as size_t
7
+ */
8
+ size_t list_len_recursion (const list_t * h )
9
+ {
10
+ if (h != NULL )
11
+ return (1 + list_len_recursion (h -> next ));
12
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+ #include "lists.h"
5
+
6
+ /**
7
+ * main - check the code for Holberton School students.
8
+ *
9
+ * Return: Always 0.
10
+ */
11
+ int main (void )
12
+ {
13
+ list_t * head ;
14
+ list_t * new ;
15
+ list_t hello = {"World" , 5 , NULL };
16
+ size_t n ;
17
+
18
+ head = & hello ;
19
+ new = malloc (sizeof (list_t ));
20
+ if (new == NULL )
21
+ {
22
+ printf ("Error\n" );
23
+ return (1 );
24
+ }
25
+ new -> str = strdup ("Hello" );
26
+ new -> len = 5 ;
27
+ new -> next = head ;
28
+ head = new ;
29
+ n = list_len_recursion (head );
30
+ printf ("-> %lu elements\n" , n );
31
+ free (new -> str );
32
+ free (new );
33
+ return (0 );
34
+ }
Original file line number Diff line number Diff line change 10
10
list_t * add_node (list_t * * head , const char * str )
11
11
{
12
12
list_t * temp ;
13
- int nodes = 0 ;
13
+ int length = 0 ;
14
14
15
15
temp = malloc (sizeof (list_t ));
16
16
if (temp == NULL )
17
17
return (NULL );
18
18
19
- while (str [nodes ])
20
- nodes ++ ;
19
+ while (str [length ])
20
+ length ++ ;
21
21
22
- temp -> len = nodes ;
22
+ temp -> len = length ;
23
23
temp -> str = strdup (str );
24
24
temp -> next = * head ;
25
25
* head = temp ;
Original file line number Diff line number Diff line change 10
10
list_t * add_node_end (list_t * * head , const char * str )
11
11
{
12
12
list_t * new_node , * temp ;
13
- unsigned int nodes = 0 ;
13
+ unsigned int length = 0 ;
14
14
15
15
new_node = malloc (sizeof (list_t ));
16
16
if (new_node == NULL )
17
17
return (NULL );
18
18
19
- while (str [nodes ])
20
- nodes ++ ;
19
+ while (str [length ])
20
+ length ++ ;
21
21
22
- new_node -> len = nodes ;
22
+ new_node -> len = length ;
23
23
new_node -> str = strdup (str );
24
24
if (* head == NULL )
25
25
{
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ typedef struct list_s
20
20
21
21
size_t print_list (const list_t * h );
22
22
size_t list_len (const list_t * h );
23
+ size_t list_len_recursion (const list_t * h );
23
24
list_t * add_node (list_t * * head , const char * str );
24
25
list_t * add_node_end (list_t * * head , const char * str );
25
26
void free_list (list_t * head );
You can’t perform that action at this time.
0 commit comments