-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |