diff --git a/0x11-singly_linked_lists/1-list_len.c b/0x11-singly_linked_lists/1-list_len.c new file mode 100644 index 0000000..272302e --- /dev/null +++ b/0x11-singly_linked_lists/1-list_len.c @@ -0,0 +1,15 @@ +#include "lists.h" +/** + * list_len - counts all elements in a list_t list. + * @h: list to be counted. + * Return: returns number of items in list. + */ +size_t list_len(const list_t *h) +{ + int i; + + for (i = 0; h != (NULL); h = h->next, i++) + ; + + return (i); +} diff --git a/0x11-singly_linked_lists/1-main.c b/0x11-singly_linked_lists/1-main.c new file mode 100644 index 0000000..74c91f9 --- /dev/null +++ b/0x11-singly_linked_lists/1-main.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include "lists.h" + +/** + * main - check the code for Holberton School students. + * + * Return: Always 0. + */ +int main(void) +{ + list_t *head; + list_t *new; + list_t hello = {"World", 5, NULL}; + size_t n; + + head = &hello; + new = malloc(sizeof(list_t)); + if (new == NULL) + { + printf("Error\n"); + return (1); + } + new->str = strdup("Hello"); + new->len = 5; + new->next = head; + head = new; + n = list_len(head); + printf("-> %lu elements\n", n); + free(new->str); + free(new); + return (0); +} diff --git a/0x11-singly_linked_lists/b b/0x11-singly_linked_lists/b new file mode 100755 index 0000000..b23c8c3 Binary files /dev/null and b/0x11-singly_linked_lists/b differ diff --git a/0x11-singly_linked_lists/lists.h b/0x11-singly_linked_lists/lists.h index 0e05a36..43eacde 100644 --- a/0x11-singly_linked_lists/lists.h +++ b/0x11-singly_linked_lists/lists.h @@ -19,5 +19,6 @@ typedef struct list_s } list_t; size_t print_list(const list_t *h); +size_t list_len(const list_t *h); #endif