-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3-add_node_end.c
More file actions
44 lines (36 loc) · 760 Bytes
/
3-add_node_end.c
File metadata and controls
44 lines (36 loc) · 760 Bytes
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lists.h"
/**
* add_node_end - adds a new node at the end
* of a list_t list.
* @head: head of the linked list.
* @str: string to store in the list.
* Return: address of the head.
*/
list_t *add_node_end(list_t **head, const char *str)
{
list_t *new_node, *current_node;
size_t n;
new_node = malloc(sizeof(list_t));
if (new_node == NULL)
return (NULL);
new_node->str = strdup(str);
for (n = 0; str[n]; n++)
;
new_node->len = n;
new_node->next = NULL;
current_node = *head;
if (current_node == NULL)
{
*head = new_node;
}
else
{
while (current_node->next != NULL)
current_node = current_node->next;
current_node->next = new_node;
}
return (*head);
}