-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.c
More file actions
334 lines (273 loc) · 6.02 KB
/
lists.c
File metadata and controls
334 lines (273 loc) · 6.02 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "lists.h"
/**
* add_node_end - adds a new node at the end of a list_s list
* @head: pointer to the address of the first node
* @name: the value of the name field of the new node
* @value: the value of the value field of the new node
*
* Return: head of the new list. Otherwise NULL
*/
list_t *add_node_end(list_t **head, const char *name, const char *value)
{
list_t *trav, *new;
if (head == NULL || name == NULL || value == NULL)
return (NULL);
trav = *head;
/* create the new node */
new = create_node(name, value);
if (new == NULL)
return (NULL);
/* if the list has no elements */
if (trav == NULL)
{
*head = new;
return (*head);
}
/* traverse to the end of the list */
while (trav->next != NULL)
trav = trav->next;
trav->next = new;
return (*head);
}
/**
* list_len - finds the number of elements in a linked list_t list
* @h: pointer to a list_t
*
* Return: number of elements in the list_t
*/
size_t list_len(const list_t *h)
{
size_t count = 0;
while (h != NULL)
{
count++;
h = h->next;
}
return (count);
}
/**
* print_list - prints all the elements of a list_t list in a given format
* @h: pointer to a list_t
*
* Return: number of nodes in given list_t
*/
size_t print_list(const list_t *h)
{
size_t count = 0;
while (h != NULL)
{
printf("%s\n", h->name);
count++;
h = h->next;
}
return (count);
}
/**
* create_node - creates a new list_t node
* @name: value of the name field
* @value: value of the value field
*
* Return: address of new node. Otherwise NULL
*/
list_t *create_node(const char *name, const char *value)
{
list_t *new;
if (name == NULL || value == NULL)
return (NULL);
new = malloc(sizeof(list_t));
if (new == NULL)
return (NULL);
new->name = _strdup(name);
new->value = _strdup(value);
new->next = NULL;
return (new);
}
/**
* free_node - frees a list_t node
* @h: pointer to list_t node
*/
void free_node(list_t *h)
{
if (h == NULL)
return;
if (h->name)
free(h->name);
if (h->value)
free(h->value);
free(h);
h = NULL;
}
/**
* free_list - frees a list_t list
* @head: pointer to list_t head
*/
void free_list(list_t **head)
{
list_t *current;
if (head == NULL)
return;
while (*head != NULL)
{
current = *head;
*head = (*head)->next;
free_node(current);
}
}
/**
* list_to_array - converts a given list of strings into an array of strings
* @h: head of given list
*
* Return: an array of strings. Otherwise NULL
*/
char **list_to_array(const list_t *h)
{
char **array;
unsigned int i, length;
if (h == NULL)
return (NULL);
/* allocate space for array */
length = list_len(h);
array = malloc(sizeof(char *) * (length + 1));
if (array == NULL)
return (NULL);
for (i = 0; i < length && h; i++, h = h->next)
array[i] = _strdup(h->name);
array[length] = NULL;
return (array);
}
/**
* delete_node_name - deletes the node at given name of a linked list
* @head: pointer to the pointer of the 1st node in the list
* @name: the name value of the node that should be deleted
*
* Return: 1 is successful. Otherwise 0
*/
int delete_node_name(list_t **head, const char *name)
{
list_t *current, *del;
/* if list does not exist or list is empty or name is NULL */
if (head == NULL || *head == NULL || name == NULL)
return (0);
current = *head;
/* if you are deleting the 1st node */
if (_strcmp((*head)->name, name) == 0)
{
*head = (*head)->next;
free_node(current);
return (1);
}
/* go to the node just before the node we are supposed to delete */
while (current->next && _strcmp(current->next->name, name))
current = current->next;
/* delete the node */
if (current->next)
{
del = current->next;
current->next = current->next->next;
free_node(del);
}
return (1);
}
/**
* delete_node_index - deletes the node at given name of a linked list
* @head: pointer to the pointer of the 1st node in the list
* @index: the index of the node that should be deleted
*
* Return: 0 is successful. Otherwise -1
*/
int delete_node_index(list_t **head, int index)
{
list_t *current, *del;
int i;
/* if list does not exist or list is empty */
if (head == NULL || *head == NULL)
return (-1);
current = *head;
/* if you are deleting the 1st node */
if (index == 0)
{
*head = (*head)->next;
free_node(current);
return (0);
}
/* go to the node just before the node we are supposed to delete */
for (i = 0; (i < index - 1) && current->next; i++)
current = current->next;
/* delete the node */
if (current->next && i == index - 1)
{
del = current->next;
current->next = current->next->next;
free_node(del);
}
return (0);
}
/**
* find_name - finds the index of a node with the given name in a list_t
* @h: head of list_t
* @name: name of node
*
* Return: index of node if found. Otherwise -1
*/
int find_name(list_t *h, const char *name)
{
list_t *trav = h;
int index = 0;
if (h == NULL || name == NULL)
return (-1);
/* find matching node */
while (trav && (_strcmp(trav->name, name)))
{
index++;
trav = trav->next;
}
return (trav ? index : -1);
}
/**
* update_value - updates value field list_t node at given index
* @h: head of list_t
* @index: index of node to update
* @value: new value
*
* Return: 0 success. -1 Otherwise
*/
int update_value(list_t *h, int index, const char *value)
{
list_t *trav = h;
if (h == NULL || index < 0 || value == NULL)
return (-1);
/* find node to update */
while (index && trav)
{
index--;
trav = trav->next;
}
/* update node */
if (trav)
{
free(trav->value);
trav->value = _strdup(value);
return (0);
}
return (-1);
}
/**
* split_string - splits a given string using a given delimiter
* @str: string to split
* @delim: delimiter
*
* Return: list of strings
*/
list_t *split_string(char *str, char *delim)
{
char *next_str;
list_t *strings = NULL;
if (str == NULL || delim == NULL)
return (NULL);
/* get first string */
add_node_end(&strings, _strtok(str, delim), "");
/* get all other strings */
while ((next_str = _strtok(NULL, delim)))
add_node_end(&strings, next_str, "");
return (strings);
}