-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_node_end.c
44 lines (39 loc) · 911 Bytes
/
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
#include "main.h"
/**
* add_node_end - adds a new node at the end of a linked list
* @head: pointer to pointer to the beginning of the list
* @str: pointer to a string
*
* Return: the address of new element or NULL if it fails
*/
list_t *add_node_end(list_t **head, const char *str)
{
/* declare variables */
list_t *newNode, *tempNode;
/* initialize variables */
newNode = NULL;
tempNode = NULL;
/* request memory allocation on heap */
newNode = malloc(sizeof(list_t));
if (newNode == NULL)
return (newNode);
/* assign values to new node */
if (str)
{
newNode->str = strdup(str);
if (newNode->str == NULL && errno == ENOMEM)
return (NULL);
newNode->len = strlen(str);
}
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else
{
tempNode = *head;
while (tempNode->next != NULL)
tempNode = tempNode->next;
tempNode->next = newNode;
}
return (newNode);
}