-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
153 lines (140 loc) · 2.48 KB
/
list.c
File metadata and controls
153 lines (140 loc) · 2.48 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
#include "hsh.h"
#include <stdlib.h>
/**
* to_array - node list to array
*
* @h: head node
*
* Return: array ponter or null
*/
char **to_array(node_t *h)
{
char **arr = NULL;
node_t *node = h;
int i = 0;
arr = malloc(sizeof(char *) * 1);
while (node != NULL)
{
arr = _realloc(arr, sizeof(char *) * (i + 1), sizeof(char *) * (i + 2));
arr[i] = malloc(
sizeof(char) * (_strlen(node->key) + _strlen(node->value) + 2)
);
arr[i] = _strcpy(arr[i], node->key);
arr[i] = _strcat(arr[i], "=");
arr[i] = _strcat(arr[i], node->value);
node = node->next;
i++;
}
arr[i] = NULL;
return (arr);
}
/**
* print_list - prints all the elements of a node_t list.
*
* @h: list to print
*
* Return: the number of nodes
*/
size_t print_list(const node_t *h)
{
int len = 0;
const node_t *node = h;
while (node != NULL)
{
_puts(node->key);
_putchar('=');
_puts(node->value);
_putchar('\n');
len++;
node = node->next;
}
return (len);
}
/**
* get_node - returns node with key
*
* @head: start node
* @key: key to check
*
* Return: the nth node
*/
node_t *get_node(node_t *head, const char *key)
{
node_t *node = head;
while (node != NULL && _strcmp(key, node->key) != 0)
{
node = node->next;
}
return (node);
}
/**
* pop_node - remove a node
*
* @head: start node
* @key: key to check
*
* Return: 0 on success, 1 on failure
*/
size_t pop_node(node_t **head, const char *key)
{
size_t i = 0;
node_t *tmp, *old = NULL;
tmp = *head;
while (tmp != NULL && _strcmp(key, tmp->key) != 0)
{
old = tmp;
tmp = tmp->next;
i++;
}
if (tmp == NULL)
return (1);
if (old != NULL)
old->next = tmp->next;
else
*head = tmp->next;
free(tmp->key);
free(tmp->value);
free(tmp);
return (0);
}
/**
* add_node_end - adds a new node at the end of a listint_t list.
*
* @head: start node of the list
* @key: node key
* @value: node value
*
* Return: the address of the new element, or NULL if it failed
*/
node_t *add_node_end(node_t **head, const char *key, const char *value)
{
node_t *new, *tmp;
new = malloc(sizeof(node_t));
if (new == NULL)
return (NULL);
new->key = _strdup(key);
new->value = _strdup(value);
new->next = NULL;
if (*head == NULL)
{
*head = new;
return (new);
}
tmp = *head;
while (tmp->next != NULL && _strcmp(key, tmp->key) != 0)
{
tmp = tmp->next;
}
if (_strcmp(key, tmp->key) == 0)
{
free_list(&new);
free(tmp->value);
tmp->value = _strdup(value);
new = tmp;
}
else
{
tmp->next = new;
}
return (new);
}