-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy path2-add_node.c
More file actions
45 lines (37 loc) · 740 Bytes
/
2-add_node.c
File metadata and controls
45 lines (37 loc) · 740 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
45
/*
* File: 2-add_node.c
* Auth: Brennan D Baraban
*/
#include "lists.h"
#include <string.h>
/**
* add_node - Adds a new node at the beginning
* of a list_t list.
* @head: A pointer to the head of the list_t list.
* @str: The string to be added to the list_t list.
*
* Return: If the function fails - NULL.
* Otherwise - the address of the new element.
*/
list_t *add_node(list_t **head, const char *str)
{
char *dup;
int len;
list_t *new;
new = malloc(sizeof(list_t));
if (new == NULL)
return (NULL);
dup = strdup(str);
if (dup == NULL)
{
free(new);
return (NULL);
}
for (len = 0; str[len];)
len++;
new->str = dup;
new->len = len;
new->next = *head;
*head = new;
return (new);
}