-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3-add_node_end.c
63 lines (54 loc) · 970 Bytes
/
3-add_node_end.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lists.h"
/**
* add_node_end - Adds a new node at the end of a list
* @head: The original linked list
* @str: The string to add to the node
*
* Return: The address of the new list or NULL if it failed
*/
list_t *add_node_end(list_t **head, const char *str)
{
list_t *new_list, *temp;
if (str != NULL)
{
new_list = malloc(sizeof(list_t));
if (new_list == NULL)
return (NULL);
new_list->str = strdup(str);
new_list->len = _strlen(str);
new_list->next = NULL;
if (*head == NULL)
{
*head = new_list;
return (*head);
}
else
{
temp = *head;
while (temp->next)
temp = temp->next;
temp->next = new_list;
return (temp);
}
}
return (NULL);
}
/**
* _strlen - Returns the length of a string
* @s: String to count
*
* Return: String length
*/
int _strlen(const char *s)
{
int c = 0;
while (*s)
{
s++;
c++;
}
return (c);
}