-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_linked_lists.c
More file actions
73 lines (64 loc) · 1.24 KB
/
f_linked_lists.c
File metadata and controls
73 lines (64 loc) · 1.24 KB
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
64
65
66
67
68
69
70
71
72
73
#include "shell.h"
/**
* print_list - Print the lists
* @h: list with format list_t
* Return: number of nodes of the list
*/
size_t print_list(const list_t *h)
{
unsigned int number_nodes = 0;
while (h != NULL)
{
if (h->str)
{
_puts(num_to_str(h->len));
_puts(" ");
_puts(h->str);
_puts("\n");
}
else
_puts("[0] (nil)\n");
h = h->next;
number_nodes++;
}
return (number_nodes);
}
/**
* add_node_end - add a new node at the end of the linked list
* @head: pointer to the first node
* @str: content of the string for the first node
* Return: pointer to the first node
*/
list_t *add_node_end(list_t **head, const char *str)
{
list_t *end_node, *tmp;
end_node = malloc(sizeof(list_t));
if (end_node == NULL)
return (NULL);
end_node->str = _strdup(str);
end_node->len = _strlen(str);
end_node->next = NULL;
if (*head == NULL)
{
*head = end_node;
return (*head);
}
tmp = *head;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = end_node;
return (end_node);
}
/**
* free_list - functions that free a linkedlist
* @head: head of first node
* Return: void -> free the linked list
*/
void free_list(list_t *head)
{
if (head == NULL)
return;
free_list(head->next);
free(head->str);
free(head);
}