Skip to content

Commit

Permalink
adding 1, list_len
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfosse committed Mar 24, 2017
1 parent 3c4747a commit 6d075b9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 0x11-singly_linked_lists/1-list_len.c
Original file line number Diff line number Diff line change
@@ -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);
}
34 changes: 34 additions & 0 deletions 0x11-singly_linked_lists/1-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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);
}
Binary file added 0x11-singly_linked_lists/b
Binary file not shown.
1 change: 1 addition & 0 deletions 0x11-singly_linked_lists/lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6d075b9

Please sign in to comment.