-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist_f.c
More file actions
92 lines (85 loc) · 1.25 KB
/
list_f.c
File metadata and controls
92 lines (85 loc) · 1.25 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "shell_head.h"
/**
* add_node - function adds a new node a list
* @head: head of list
* @str: string to put in new node
*
* Return: 1 on success, 0 otherwise
*/
int add_node(list_t **head, char *str)
{
list_t *new;
list_t *tmp = *head;
new = malloc(sizeof(list_t));
if (!new || !head || !str)
return (0);
new->str = _strdup(str);
if (!new->str)
{
free(new);
return (0);
}
new->len = _strlen(str);
new->next = NULL;
if (*head == NULL)
{
*head = new;
return (1);
}
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = new;
return (1);
}
/**
* free_list - function frees an entire list
* @head: head of linked list
*
* Return: none
*/
void free_list(list_t *head)
{
list_t *tmp;
while (head != NULL)
{
tmp = head->next;
free(head->str);
free(head);
head = tmp;
}
}
/**
* list_len - functions finds and returns amount of nodes in a list
* @h: head of list
*
* Return: count of nodes
*/
size_t list_len(list_t *h)
{
size_t count = 0;
if (!h)
return (0);
while (h)
{
count++;
h = h->next;
}
return (count);
}
/**
* print_list - function prints a list
* @h: head of linked list
*
* Return: none
*/
void print_list(list_t *h)
{
while (h)
{
_puts(h->str, 1);
_puts("\n", 1);
h = h->next;
}
}