-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.c
More file actions
276 lines (256 loc) · 6.84 KB
/
linkedlist.c
File metadata and controls
276 lines (256 loc) · 6.84 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
/* =============================================================================
linkedlist.c
Linked list implementation that allows any data type to be stored.
Reference:
- Originally written for COMP20003 Assignment 2, 2022, altered to fit
this project.
- Implementation of linked list functions inspired by Alistair Moffat
from "Programming, Problem Solving, and Abstraction with C".
Author: David Sha
============================================================================= */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
list_t *create_empty_list() {
/* Creates an empty linked list.
*/
list_t *list;
list = (list_t *)malloc(sizeof(*list));
assert(list);
list->head = list->foot = NULL;
return list;
}
list_t *create_list(node_t *head, node_t *foot) {
/* Create a linked list given the head and foot.
*/
assert(head && foot);
list_t *new = create_empty_list();
new->head = head;
new->foot = foot;
return new;
}
int is_empty_list(list_t *list) {
/* Checks if list is empty.
*/
assert(list);
return list->head == NULL;
}
void free_list(list_t *list, void (*free_data)(void *data)) {
/* Free the list by freeing all nodes and its contents.
Give a function pointer to free the data. If no function
pointer is given, the data will not be freed.
*/
assert(list);
node_t *curr, *prev;
curr = list->head;
while (curr) {
prev = curr;
curr = curr->next;
if (free_data) {
free_data(prev->data);
}
free(prev);
}
free(list);
}
list_t *prepend(list_t *list, void *data) {
/* Prepend to the list i.e. add to head of linked list.
*/
assert(list && data);
node_t *new;
new = (node_t *)malloc(sizeof(*new));
assert(new);
new->data = data;
new->prev = NULL;
new->next = list->head;
if (list->head) {
list->head->prev = new;
} else {
/* this is the first insert into list */
list->foot = new;
}
list->head = new;
return list;
}
list_t *append(list_t *list, void *data) {
/* Append to the list i.e. add to foot of linked list.
*/
assert(list && data);
node_t *new;
new = (node_t *)malloc(sizeof(*new));
assert(new);
new->data = data;
new->next = NULL;
new->prev = list->foot;
if (list->foot) {
list->foot->next = new;
} else {
/* this is the first insert into list */
list->head = new;
}
list->foot = new;
return list;
}
int list_len(list_t *list) {
/* Get the linked list length.
*/
assert(list);
int len = 0;
node_t *curr;
curr = list->head;
while (curr) {
len++;
curr = curr->next;
}
return len;
}
void remove_node(list_t *list, node_t *node) {
/* Remove a node from the list.
*/
assert(list && node);
if (node->prev == NULL) {
/* removing the first node in the list */
list->head = node->next;
if (list->foot == node) {
/* also removing the last node in the list */
list->foot = NULL;
} else {
/* set the prev pointer of the new head */
list->head->prev = NULL;
}
} else {
/* removing a node in the middle or end of the list */
node->prev->next = node->next;
if (list->foot == node) {
/* also removing the last node in the list */
list->foot = node->prev;
} else {
/* set the prev pointer of the next node */
node->next->prev = node->prev;
}
}
free(node);
}
void *remove_data(list_t *list, void *data) {
/* Remove data from the list. If data is not in the list, return NULL.
*/
assert(list && data);
node_t *curr;
curr = list->head;
while (curr) {
if (curr->data == data) {
remove_node(list, curr);
return data;
}
curr = curr->next;
}
return NULL;
}
void *move_data(void *data, list_t *from, list_t *to) {
/* Move data from one list to another.
*/
assert(data && from && to);
void *removed = remove_data(from, data);
if (removed) {
append(to, removed);
}
return removed;
}
void *pop(list_t *list) {
/* Pop from the head of list.
*/
assert(list);
node_t *head = list->head;
if (head) {
list->head = head->next;
if (list->foot == head) {
/* also removing the last node in the list */
list->foot = NULL;
} else {
/* set the prev pointer of the new head */
list->head->prev = NULL;
}
void *data = head->data;
free(head);
return data;
}
return NULL;
}
void print_list(list_t *list, void (*print_data)(void *)) {
/* Print a linked list in the format [data1, data2, ...].
*/
assert(list);
printf("[");
for (node_t *curr = list->head; curr; curr = curr->next) {
print_data(curr->data);
if (curr->next) {
printf(", ");
}
}
printf("]\n");
}
list_t *insert_prev(list_t *list, node_t *node, void *data) {
/* Insert data before the given node.
*/
assert(list && node && data);
node_t *new;
new = (node_t *)malloc(sizeof(*new));
assert(new);
new->data = data;
new->prev = node->prev;
new->next = node;
if (node->prev) {
node->prev->next = new;
} else {
/* this is the first insert into list */
list->head = new;
}
node->prev = new;
return list;
}
list_t *insert_next(list_t *list, node_t *node, void *data) {
/* Insert data after the given node.
*/
assert(list && node && data);
node_t *new;
new = (node_t *)malloc(sizeof(*new));
assert(new);
new->data = data;
new->next = node->next;
new->prev = node;
if (node->next) {
node->next->prev = new;
} else {
/* this is the first insert into list */
list->foot = new;
}
node->next = new;
return list;
}
void *find_node(list_t *list, void *data, int (*cmp)(void *, void *)) {
/* Find data in the list. If data is not in the list, return NULL.
*/
assert(list && data && cmp);
for (node_t *curr = list->head; curr; curr = curr->next) {
if (cmp(curr->data, data) == 0) {
return curr;
}
}
return NULL;
}
int cmp_addr(void *a, void *b) {
/* Compare two addresses. If a == b, return 0. Otherwise, return 1.
*/
return a != b;
}
void copy_list(list_t *from, list_t *to) {
/* Copy the data from one list to another. The data is not copied, only
the pointers to the data are copied.
*/
for (node_t *curr = from->head; curr; curr = curr->next) {
void *data = curr->data;
append(to, data);
}
}