-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-add_node.c
More file actions
39 lines (32 loc) · 877 Bytes
/
2-add_node.c
File metadata and controls
39 lines (32 loc) · 877 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
#include "lists.h"
/**
* add_node - Add a new node at the beginning of a singly linked list
* @head: Pointer to head in main function
* @str: String to be duplicated
* Return: Address of the new element, or NULL if it failed
*/
list_t *add_node(list_t **head, const char *str)
{
list_t *new_node = NULL;
/* verify if the argument passed exist */
if (head == NULL)
return (NULL);
/* dynamically allocate memory for new_node */
new_node = malloc(sizeof(list_t));
if (new_node == NULL)
return (NULL);
/* assign values to new_node */
new_node->str = strdup(str);
new_node->len = strlen(str);
new_node->next = NULL;
/* verify if the list exist (is no empty) */
if (*head == NULL)
{
*head = new_node;
return (new_node);
}
/* insert new_node at the beginning and make it the head */
new_node->next = *head;
*head = new_node;
return (new_node);
}