Skip to content

Commit 07c0217

Browse files
make up my codes and use recursion
1 parent 0cf6953 commit 07c0217

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0x12-singly_linked_lists/11-main.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

0x12-singly_linked_lists/2-add_node.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
list_t *add_node(list_t **head, const char *str)
1111
{
1212
list_t *temp;
13-
int nodes = 0;
13+
int length = 0;
1414

1515
temp = malloc(sizeof(list_t));
1616
if (temp == NULL)
1717
return (NULL);
1818

19-
while (str[nodes])
20-
nodes++;
19+
while (str[length])
20+
length++;
2121

22-
temp->len = nodes;
22+
temp->len = length;
2323
temp->str = strdup(str);
2424
temp->next = *head;
2525
*head = temp;

0x12-singly_linked_lists/3-add_node_end.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
list_t *add_node_end(list_t **head, const char *str)
1111
{
1212
list_t *new_node, *temp;
13-
unsigned int nodes = 0;
13+
unsigned int length = 0;
1414

1515
new_node = malloc(sizeof(list_t));
1616
if (new_node == NULL)
1717
return (NULL);
1818

19-
while (str[nodes])
20-
nodes++;
19+
while (str[length])
20+
length++;
2121

22-
new_node->len = nodes;
22+
new_node->len = length;
2323
new_node->str = strdup(str);
2424
if (*head == NULL)
2525
{

0x12-singly_linked_lists/lists.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct list_s
2020

2121
size_t print_list(const list_t *h);
2222
size_t list_len(const list_t *h);
23+
size_t list_len_recursion(const list_t *h);
2324
list_t *add_node(list_t **head, const char *str);
2425
list_t *add_node_end(list_t **head, const char *str);
2526
void free_list(list_t *head);

0 commit comments

Comments
 (0)