-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_lists.c
262 lines (231 loc) · 6.03 KB
/
linked_lists.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
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
#include "linked_lists.h"
// library that allows for a linked list of any type to be created for learning purposes
int ll_print(LinkedList* list, void print(const void*))
{
ListNode* current = list->head;
while (current != NULL)
{
print(current->value);
current = current->next;
}
putchar('\n');
return EXIT_SUCCESS;
}
// inserts a value at index in a linked list
// inserting at index -1 inserts at the end of the list
int ll_insert(LinkedList* list, void* value, const int index)
{
if (index > list->itemCount || index < -1 || list == NULL)
{
return EXIT_FAILURE;
}
list->itemCount++;
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->value = (void**)malloc(sizeof(void*));
if (node == NULL)
{
return EXIT_FAILURE;
}
memcpy(node->value, value, list->dataSize); // assign value
if (list->head == NULL)
{
// list is empty
list->head = node;
return EXIT_SUCCESS;
}
// insert at head
if (index == 0)
{
node->next = list->head;
list->head = node;
}
else if (index == -1) // insert at tail
{
ListNode* current = list->head;
// travel to end of list
while (current->next != NULL)
{
current = current->next;
}
current->next = node;
}
else
{
ListNode* current = list->head;
// travel to node that comes before index
for (int i = 0; i < index - 1; i++)
{
if (current->next == NULL)
{
break;
}
current = current->next;
}
node->next = current->next;
current->next = node;
}
return EXIT_SUCCESS;
}
int ll_insert_int(LinkedList* list, int value, const int index)
{
return ll_insert(list, &value, index);
}
int ll_insert_str(LinkedList* list, char* value, const int index)
{
return ll_insert(list, (void*)value, index);
}
int ll_insert_flt(LinkedList* list, float value, const int index)
{
return ll_insert(list, &value, index);
}
int ll_insert_dbl(LinkedList* list, double value, const int index)
{
return ll_insert(list, &value, index);
}
int ll_insert_chr(LinkedList* list, char value, const int index)
{
return ll_insert(list, &value, index);
}
int ll_delete(LinkedList* list, const int index)
{
if (list->itemCount - 1 < 0 || index >= list->itemCount || index < -1)
{
return EXIT_FAILURE;
}
if (index == 0 || list->itemCount == 0)
{
ListNode* temp = list->head;
list->head = list->head->next;
free(temp->value);
temp->value = NULL;
free(temp);
temp = NULL;
}
else if (index == -1)
{
ListNode* current = list->head;
while (current->next->next != NULL) // travel to penultimate node
{
current = current->next;
}
// delete final node
free(current->next->value);
current->next->value = NULL;
free(current->next);
current->next = NULL;
}
else
{
ListNode* current = list->head;
// travel to node that comes before index
for (int i = 0; i < index - 1; i++)
{
if (current->next == NULL)
{
break;
}
current = current->next;
}
// store node after index
ListNode* temp = current->next;
// isolate node to be freed by connect node at index with node after node to be freed
current->next = current->next->next;
free(temp->value);
temp->value = NULL;
free(temp);
temp = NULL;
}
return EXIT_SUCCESS;
}
int ll_reverse(LinkedList* list)
{
if (list->head == NULL)
{
// list is empty
return EXIT_FAILURE;
}
ListNode* current = list->head->next;
ListNode* previous = list->head;
previous->next = NULL;
while (current != NULL)
{
ListNode* next = current->next;
current->next = previous;
previous = current;
current = next;
}
list->head = previous;
return EXIT_SUCCESS;
}
// performs free_item() on each item in the linked list
// if free_item is NULL will default to basic free function
int ll_free(LinkedList* list, void (*free_item)(void*))
{
// allow override of defualt free function with user's own
// this allows for larger free functions for complex structs
if (free_item == NULL)
{
free_item = default_free;
}
if (list->head == NULL)
{
free(list);
list = NULL;
return EXIT_FAILURE;
}
ListNode* previous = list->head;
list->head = list->head->next;
while (list->head != NULL)
{
free_item(previous->value);
free(previous);
previous = list->head;
list->head = list->head->next;
}
free_item(previous->value);
free(previous);
free(list);
previous = NULL;
list = NULL;
return EXIT_SUCCESS;
}
// create an empty linked list by passing NULL
// or convert any array into a linked list by converting it into void pointers
// using array_to_void_array() and passing it
// byte size (aka datatype) of items being stored needs to be specified
LinkedList* ll_create(void* values[], const size_t n, const size_t typeSize)
{
if (values == NULL && n != 0)
{
return NULL;
}
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
if (list == NULL)
{
return NULL;
}
list->dataSize = typeSize;
list->itemCount = 0;
for (size_t i = 0; i < n; i++)
{
ll_insert(list, values[i], -1);
}
if (values != NULL)
{
free_void_array(values, n);
}
return list;
}
const struct LibLinkedList_l LibLinkedList = {
.create = ll_create,
.insert = ll_insert,
.insert_int = ll_insert_int,
.insert_chr = ll_insert_chr,
.insert_dbl = ll_insert_dbl,
.insert_flt = ll_insert_flt,
.insert_str = ll_insert_str,
.print = ll_print,
.delete = ll_delete,
.reverse = ll_reverse,
.free = ll_free
};