-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
159 lines (131 loc) · 2.84 KB
/
linkedlist.c
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
#include "shell.h"
/**
* append_node - Inserts a new tail node
* @head: Pointer to current head
* @str: String to store in the new tail node
* Return: Pointer to new tail; NULL on failure
*/
list_t *append_node(list_t **head, const char *str)
{
list_t *new, *tail;
size_t len = 0;
if (!head)
return (NULL);
new = malloc(sizeof(list_t));
if (!new)
return (NULL);
new->str = _strdup(str);
if (!new->str)
{
free(new);
return (NULL);
}
while (new->str[len])
len++;
new->len = len;
new->next = NULL;
if (!*head)
{
*head = new;
return (new);
}
tail = *head;
while (tail->next)
tail = tail->next;
tail->next = new;
return (new);
}
/**
* free_list - Frees all nodes in a list_t linked list
* @head: List head
* Return: Void
*/
void free_list(list_t *head)
{
list_t *node = head;
while (node)
{
free(node->str);
node = head->next;
free(head);
head = node;
}
}
/**
* split_str - splits a string into a str_list_t linked list
* !NOTE! The caller must free() the returned linked list
* @string: null terminated string to split
* @dlmtr: delimiter(s)
* Return: str_list_t linked list on success;
* NULL if string or dlmtr is NULL;
*/
str_list_t *split_str(char *const string, const char *dlmtr)
{
char *tok_ptr, *string_copy;
str_list_t *head = NULL;
if (!string || !dlmtr)
return (NULL);
string_copy = _strdup(string);
/* Create nodes for each token */
tok_ptr = _strtok(string_copy, dlmtr);
while (tok_ptr)
{
if (!append_node(&head, tok_ptr))
{
free(head);
free(string_copy);
return (NULL);
}
tok_ptr = _strtok(NULL, dlmtr);
}
free(string_copy);
return (head);
}
/**
* str_list_t_to_array - lists each string in a str_list_t in an array
* !NOTE! The caller must free() the returned array
* @head: head of the linked list
* Return: malloc'd array of pointers to strings
*/
char **str_list_t_to_array(str_list_t *head)
{
str_list_t *node;
char **argv;
size_t n_nodes, i = 0; /* argv itterator */
/* Count nodes */
node = head;
for (n_nodes = 0; node; n_nodes++)
node = node->next;
/* Create array */
argv = malloc(sizeof(char *) * (n_nodes + 1));
if (!argv)
{
free(head);
return (NULL);
}
/* Add elements to the array */
for (node = head; node; node = node->next)
argv[i++] = node->str;
argv[i] = NULL; /* NULL terminate the array */
return (argv);
}
/**
* add_node - adds a new node at the begining of list_t
* @head: double pointer to the start of list_t
* @str: string to add
* Return: pointer to the new element in the list, otherwise NULL if failed
*/
list_t *add_node(list_t **head, const char *str)
{
list_t *new_node;
if (str == NULL)
return (NULL);
new_node = malloc(sizeof(list_t));
if (new_node == NULL)
return (NULL);
new_node->str = _strdup(str);
new_node->len = _strlen(str);
new_node->next = *head;
*head = new_node;
return (new_node);
}