-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlists.c
More file actions
113 lines (108 loc) · 2.01 KB
/
Copy pathlists.c
File metadata and controls
113 lines (108 loc) · 2.01 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "shell.h"
/**
* list_len - returns the number of elements in a linked list_t list
* @h: linked List
* Return: number of elements
*/
size_t list_len(list_t *h)
{
size_t len;
len = 0;
while (h != NULL)
{
len++;
h = h->next;
}
return (len);
}
/**
* add_node - adds a new node at the beginning of a list_t list
* @head: first node in the list
* @str: second node in the list
* Return: the address of the new element, or NULL if it failed
*/
list_t *add_node(list_t **head, char *str)
{
list_t *new;
new = malloc(sizeof(list_t));
if (new == NULL)
return (NULL);
new->str = _strdup(str);
if (new->str == NULL)
return (NULL);
new->len = _strlen(str);
new->next = *head;
*head = new;
return (*head);
}
/**
* add_node_end - check the code for Holberton School students.
* @head: pointer to first node in linked list
* @str: data to be copied to string field of the node
* Return: the address of the new element, or NULL if it failed
*/
list_t *add_node_end(list_t **head, char *str)
{
list_t *new;
list_t *tail;
new = malloc(sizeof(list_t));
if (new == NULL)
return (NULL);
new->str = _strdup(str);
if (new->str == NULL)
return (NULL);
new->len = _strlen(str);
new->next = NULL;
if (*head == NULL)
{
*head = new;
return (new);
}
tail = *head;
while (1)
{
if (tail->next == NULL)
{
tail->next = new;
break;
}
tail = tail->next;
}
return (new);
}
/**
* free_list - frees a list_t list
* @head: linked list to free
* Return: none
*/
void free_list(list_t *head)
{
list_t *tmp;
while (1)
{
if (head == NULL)
break;
tmp = head;
head = tmp->next;
free(tmp->str);
free(tmp);
}
}
/**
* get_node - returns the node with string str
* @head: pointer to first node
* @str: string to search for
* Return: n-th node, if the node does not exist, return NULL
*/
list_t *get_node(list_t **head, char *str)
{
list_t *tmp;
tmp = *head;
while (head && tmp)
{
if (_strcmp(tmp->str, str) == 0 && tmp)
return (tmp);
tmp = tmp->next;
}
return (NULL);
}