-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlList.c
370 lines (233 loc) · 6.78 KB
/
dlList.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Hears a comment
#include <stdlib.h>
/// The struct that represents a node in the list. Each node
/// consists of the data at this node, the previous node, and
/// the next node. The data is of type void * and the next/previous
/// is of type struct node.
typedef struct node {
void * data;
struct node * previous;
struct node * next;
} Node;
// This implementation of the struct consists of the current or curser node,
// the current index or 'currIndex' of the current node in the list, the last
// node in the list, and the size of the list.
struct dListStruct {
Node * current;
size_t currIndex;
Node * last;
size_t size;
};
typedef struct dListStruct * DlList_T;
#define _DLL_IMPL_
#include <stdio.h>
#include <assert.h>
#include "dlList.h"
/// Takes the contents of a node and creates a Node * to a new node.
/// @param data the data for the node
/// @param prev the previous node to this node.
/// @param next the next node to this node.
/// @return a newly allocated Node * with the contents from the parameters
static Node * makeNode(void * data, Node * prev, Node * next) {
Node * new = (Node *) malloc(sizeof(Node));
new->data = data;
new->previous = prev;
new->next = next;
return new;
}
// Creates and allocates memory for a new DlList_T
DlList_T dll_create( void ) {
DlList_T new;
new = (DlList_T) malloc(sizeof(struct dListStruct));
if (new != 0) {
new->current = 0;
new->currIndex = 0;
new->last = 0;
new->size = 0;
}
return new;
}
// Loops through and frees all of the ndoes then frees the list itself.
void dll_destroy( DlList_T lst ) {
assert(lst != 0);
lst->current = lst->last;
while (lst->current->previous) {
lst->current = lst->current->previous;
free(lst->current->next);
}
free(lst->current);
free(lst);
}
// Frees the nodes in the list and sets the contents to null.
void dll_clear( DlList_T lst ) {
assert(lst != 0);
while (lst->current->previous) {
lst->current = lst->current->previous;
free(lst->current->next);
}
free(lst->current);
lst->current = 0;
lst->currIndex = 0;
lst->last = 0;
lst->size = 0;
}
// Moves the cursor to the indexed node in the list. Returns true
// if move is successful, false otherwise.
bool dll_move_to( DlList_T lst, int indx ) {
if (indx < 0 || indx >= lst->size) { return false; }
int direction = -1;
if (indx > lst->currIndex) {
direction = 1;
}
while(lst->currIndex != indx) {
if (direction == 1) {
lst->current = lst->current->next;
} else {
lst->current = lst->current->previous;
}
lst->currIndex += direction;
}
return true;
}
// Checks if the current node in the list is null.
int dll_has_next( DlList_T lst ) {
return lst->currIndex >= 0 && lst->currIndex < lst->size;
}
// returns the data of the current node THEN moves the cursor for the current node
// to the next node in the list.
void * dll_next( DlList_T lst ) {
Node * current = lst->current;
lst->current = lst->current->next;
lst->currIndex++;
return current->data;
}
// returns the data of the current node THEN moves the cursor for the current node
// to the previous node in the list.
void * dll_prev( DlList_T lst ) {
Node * current = lst->current;
lst->current = lst->current->previous;
lst->currIndex--;
return current->data;
}
// returns the size of the list.
int dll_size( DlList_T lst ) {
return lst->size;
}
// Adds a new node to the end of the list.
void dll_append( DlList_T lst, void *data ) {
Node * toAdd = makeNode(data, 0, 0);
toAdd->previous = lst->last;
lst->last->next = toAdd;
lst->current = toAdd;
lst->last = lst->current;
lst->size += 1;
}
// If index is valid, it inserts a new node at that index in the list.
void dll_insert_at( DlList_T lst, int indx, void *data ) {
int index = dll_size(lst) - 1;
while (index >= 0) {
if (index == indx) {
Node * newPrevious = lst->current->previous;
Node * newNext = lst->current;
lst->current = makeNode(data, newPrevious, newNext);
lst->size++;
}
lst->current = lst->current->previous;
}
}
// returns the data at the node specified by the index given.
void * dll_get( DlList_T lst, int indx ) {
if (indx >= lst->size || indx < 0) {
return (void*) 0;
}
int index = lst->size - 1;
Node * current = lst->current;
while (index >= 0) {
if (index == indx) {
return current->data;
}
index--;
current = current->previous;
}
return (void*) 0;
}
// creates a new node for the given data and inserts it at the index given.
// Returns the data of the old node that it replaced.
void * dll_set( DlList_T lst, int indx, void *data ) {
Node * newNode = makeNode(data, 0, 0);
int index = lst->size - 1;
Node * current = lst->current;
while (index >= 0) {
if (index == indx) {
Node * newPrevious = current->previous;
Node * newNext = current-> next;
newNode->previous = newPrevious;
newNode->next = newNext;
free(lst->current);
lst->current = newNode;
return current->data;
}
index--;
current = current->previous;
}
return (void*) 0;
}
// Removes the item at the given index and returns its data value.
void * dll_pop( DlList_T lst, int indx ) {
void * toReturn = dll_get(lst, indx);
int index = lst->size - 1;
Node * current = lst->current;
while (index >= 0) {
if (index == indx) {
lst->size--;
free(current);
return toReturn;
}
index--;
current = current->previous;
}
return (void *) 0;
}
// returns the index of the node which has the same data as the one given.
int dll_index( DlList_T lst, void *data ) {
int toReturn = lst->size - 1;
Node * current = lst->current;
while (toReturn >= 0) {
if (current->data == data) {
return toReturn;
}
toReturn--;
}
return -1;
}
/// Prints the list to stdout.
/// @param lst the already created list.
void printList(DlList_T lst) {
printf("Null, ");
for (int i = 0; i < lst->size; i++) {
printf("%d, ", (int)dll_get(lst, i));
}
printf("Null");
}
// returns true if lst->size == 0, false otherwise
bool dll_empty( DlList_T lst ) {
if (dll_size(lst)) {
return false;
}
return true;
}
int main(void) {
DlList_T lst = dll_create();
printf( "Initial list is %s\n", dll_empty(lst) ? "empty" : "not empty" );
dll_append(lst, (void *)1);
dll_append(lst, (void *)2);
dll_append(lst, (void *)3);
dll_append(lst, (void *)4);
printList(lst);
dll_insert_at(lst, 2, (void *)6);
printList(lst);
printf( "List is %s\n", dll_empty(lst) ? "empty" : "not empty" );
dll_destroy(lst);
printList(lst);
return 1;
}