diff --git a/0x11-singly_linked_lists/3-add_node_end.c b/0x11-singly_linked_lists/3-add_node_end.c new file mode 100644 index 0000000..a6d535a --- /dev/null +++ b/0x11-singly_linked_lists/3-add_node_end.c @@ -0,0 +1,32 @@ +#include "lists.h" +/** + * add_node - adds new element at the end of a list. + * @head: list to add too. + * @str: element to add. + * Return: returns the adress of new element, NULL on fail. + */ +list_t *add_node_end(list_t **head, const char *str) +{ + int i; + list_t *s, *copy; + + s = malloc(sizeof(list_t)); + if (s == NULL) + return (NULL); + for (i = 0; str[i] != '\0'; i++) + ; + + s->str = strdup(str); + s->len = i; + s->next = NULL; + copy = *head; + if (*head == NULL) + { + *head = s; + return (*head); + } + for (; copy->next != NULL; copy = copy->next) + ; + copy->next = s; + return (copy->next); +} diff --git a/0x11-singly_linked_lists/3-main.c b/0x11-singly_linked_lists/3-main.c new file mode 100644 index 0000000..92caa28 --- /dev/null +++ b/0x11-singly_linked_lists/3-main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include "lists.h" + +/** + * main - check the code for Holberton School students. + * + * Return: Always 0. + */ +int main(void) +{ + list_t *head; + + head = NULL; + add_node_end(&head, "Anne"); + add_node_end(&head, "Colton"); + add_node_end(&head, "Corbin"); + add_node_end(&head, "Daniel"); + add_node_end(&head, "Danton"); + add_node_end(&head, "David"); + add_node_end(&head, "Gary"); + add_node_end(&head, "Holden"); + add_node_end(&head, "Ian"); + add_node_end(&head, "Ian"); + add_node_end(&head, "Jay"); + add_node_end(&head, "Jennie"); + add_node_end(&head, "Jimmy"); + add_node_end(&head, "Justin"); + add_node_end(&head, "Kalson"); + add_node_end(&head, "Kina"); + add_node_end(&head, "Matthew"); + add_node_end(&head, "Max"); + add_node_end(&head, "Michael"); + add_node_end(&head, "Ntuj"); + add_node_end(&head, "Philip"); + add_node_end(&head, "Richard"); + add_node_end(&head, "Samantha"); + add_node_end(&head, "Stuart"); + add_node_end(&head, "Swati"); + add_node_end(&head, "Timothy"); + add_node_end(&head, "Victor"); + add_node_end(&head, "Walton"); + print_list(head); + return (0); +} diff --git a/0x11-singly_linked_lists/d b/0x11-singly_linked_lists/d new file mode 100755 index 0000000..360ee1d Binary files /dev/null and b/0x11-singly_linked_lists/d differ diff --git a/0x11-singly_linked_lists/lists.h b/0x11-singly_linked_lists/lists.h index 294287c..7a41031 100644 --- a/0x11-singly_linked_lists/lists.h +++ b/0x11-singly_linked_lists/lists.h @@ -22,5 +22,6 @@ typedef struct list_s size_t print_list(const list_t *h); size_t list_len(const list_t *h); list_t *add_node(list_t **head, const char *str); +list_t *add_node_end(list_t **head, const char *str); #endif