Skip to content

Commit

Permalink
adding 3, added fix if head == NULL.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfosse committed Mar 24, 2017
1 parent a711e58 commit 5f5dedf
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 0x11-singly_linked_lists/3-add_node_end.c
Original file line number Diff line number Diff line change
@@ -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);
}
46 changes: 46 additions & 0 deletions 0x11-singly_linked_lists/3-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#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;

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);
}
Binary file added 0x11-singly_linked_lists/d
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 @@ -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

0 comments on commit 5f5dedf

Please sign in to comment.