Skip to content

Commit

Permalink
adding 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfosse committed Mar 24, 2017
1 parent 6d075b9 commit a711e58
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 0x11-singly_linked_lists/2-add_node.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "lists.h"
/**
* add_node - adds new element at the start 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(list_t **head, const char *str)
{
int i;
list_t *s;

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 = *head;
*head = s;
return (s);
}
46 changes: 46 additions & 0 deletions 0x11-singly_linked_lists/2-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(&head, "Alexandro");
add_node(&head, "Asaia");
add_node(&head, "Augustin");
add_node(&head, "Bennett");
add_node(&head, "Bilal");
add_node(&head, "Chandler");
add_node(&head, "Damian");
add_node(&head, "Daniel");
add_node(&head, "Dora");
add_node(&head, "Electra");
add_node(&head, "Gloria");
add_node(&head, "Joe");
add_node(&head, "John");
add_node(&head, "John");
add_node(&head, "Josquin");
add_node(&head, "Kris");
add_node(&head, "Marine");
add_node(&head, "Mason");
add_node(&head, "Praylin");
add_node(&head, "Rick");
add_node(&head, "Rick");
add_node(&head, "Rona");
add_node(&head, "Siphan");
add_node(&head, "Sravanthi");
add_node(&head, "Steven");
add_node(&head, "Tasneem");
add_node(&head, "William");
add_node(&head, "Zee");
print_list(head);
return (0);
}
Binary file added 0x11-singly_linked_lists/c
Binary file not shown.
2 changes: 2 additions & 0 deletions 0x11-singly_linked_lists/lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lists.h"
/**
* struct list_s - singly linked list
Expand All @@ -20,5 +21,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);

#endif

0 comments on commit a711e58

Please sign in to comment.