-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.c
454 lines (404 loc) · 9.96 KB
/
List.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//-----------------------------------------------------------------------------
// List.c
// Implementation file for List ADT
//-----------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include "List.h"
// structs --------------------------------------------------------------------
// private NodeObj type
typedef struct NodeObj {
int data;
struct NodeObj* next;
struct NodeObj* previous;
} NodeObj;
// private Node type
typedef NodeObj* Node;
typedef struct ListObj{
Node front;
Node back;
Node cursor;
int length;
int index;
} ListObj;
// Constructors-Destructors ---------------------------------------------------
// newNode()
// Returns reference to new Node object. Initializes next and data fields.
// Private.
Node newNode(int data) {
Node N = malloc(sizeof(NodeObj));
N->data = data;
N->next = NULL;
N->previous = NULL;
return(N);
}
// freeNode()
// Frees heap memory pointed to by *pN, sets *pN to NULL.
// Private.
void freeNode(Node* pN){
if( pN!=NULL && *pN!=NULL ){
free(*pN);
*pN = NULL;
}
}
// newList()
// Returns reference to new empty List object.
List newList(void) {
List L = malloc(sizeof(ListObj));
L->front = NULL;
L->back = NULL;
L->cursor = NULL;
L->length = 0;
L->index = -1;
return(L);
}
// freeList()
// Frees all heap memory associated with List to NULL.
void freeList(List* L) {
if(L != NULL && *L != NULL) {
while(!isEmpty(*L)) {
deleteFront(*L);
}
free(*L);
L = NULL;
}
}
// Access functions -----------------------------------------------------------
// length()
// Returns the length of L.
int length(List L) {
if(L == NULL) {
printf("List Error: calling length() on NULL List reference\n");
exit(1);
}
return(L->length);
}
// index()
// Returns the index reference for the cursor.
int index(List L) {
if(L->cursor == NULL) { //this could be a problem
return(-1);
}
return(L->index);
}
// front()
// Returns the value at the front of L.
// Pre: !isEmpty(L)
int front(List L) {
if(L == NULL) {
printf("List Error: calling front() on NULL List reference\n");
exit(1);
}
if(isEmpty(L) ){
printf("List Error: calling front() on an empty List\n");
exit(1);
}
return(L->front->data);
}
// back()
// Returns the value at the back of L.
// Pre: !isEmpty(L)
int back(List L) {
if(L == NULL) {
printf("List Error: calling back() on NULL List reference\n");
exit(1);
}
if(isEmpty(L) ){
printf("List Error: calling back() on an empty List\n");
exit(1);
}
return(L->back->data);
}
// get()
// Returns the value contained in cursor.
// Pre: !isEmpty(L)
int get(List L) {
if(L == NULL) {
printf("List Error: calling get() on NULL List reference\n");
exit(1);
}
if(isEmpty(L)){
printf("List Error: calling get() on an empty List\n");
exit(1);
}
if(L->cursor == NULL) {
printf("List Error: calling get() on undefined cursor\n");
exit(1);
}
return(L->cursor->data);
}
// equals()
// Returns true (1) if Lists match, otherwise returns false (0)
int equals(List A, List B) {
int eq = 0;
Node N = NULL;
Node M = NULL;
if(A == NULL || B == NULL) {
printf("List Error: calling equals() on NULL List reference\n");
exit(1);
}
eq = (A->length == B->length);
N = A->front;
M = B->front;
while(eq && N != NULL){
eq = (N->data == M->data);
N = N->next;
M = M->next;
}
return eq;
}
// isEmpty()
// Returns true (1) if Q is empty, otherwise returns false (0)
int isEmpty(List L){
if(L == NULL) {
printf("List Error: calling isEmpty() on NULL List reference\n");
exit(1);
}
return(L->length == 0);
}
// Manipulation procedures ----------------------------------------------------
//moveFront()
//moves cursor to the front of the List
void moveFront(List L) {
if(!isEmpty(L)) {
L->cursor = L->front;
//printf("%p\n", (void*) &L->cursor->previous);
//printf("%p\n", (void*) &L->front->previous);
//printf("%d\n", L->cursor->next->data);
//printf("%d\n", L->front->next->data);
L->index = 0;
}
}
// void moveBack()
// if List is non-empty moves cursor to the back of the list.
// otherwise does nothing
void moveBack(List L) {
if(!isEmpty(L)) {
L->cursor = L->back;
L->index = length(L) - 1;
}
}
// void movePrev()
// if List is non-empty, and cursor is not at front,
// moves cursor towards the front of the List
// if cursor is undefined, does nothing
void movePrev(List L) {
if(!isEmpty(L)) {
if(index(L) == 0) {
L->cursor = NULL;
L->index = -1;
} else {
L->cursor = L->cursor->previous;
L->index--;
}
}
}
// void moveNext()
// if List is non-empty, and cursor is not at back,
// moves cursor towards the back of the List
// if cursor is undefined, does nothing
void moveNext(List L) {
if(!isEmpty(L)) {
if(index(L) == length(L) - 1) {
L->cursor = NULL;
L->index = -1;
} else {
L->cursor = L->cursor->next;
//printf("%d\n", L->cursor->previous->next->data);
L->index++;
}
}
}
// append()
// Places new data element at the end of L
void append(List L, int data) {
Node N = newNode(data);
if(L == NULL){
printf("List Error: calling append() on NULL List reference\n");
exit(1);
}
if(isEmpty(L)) {
L->front = L->back = N;
} else {
L->back->next = N;
N->previous = L->back;
L->back = N;
}
L->length++;
}
// prepend()
// Places new data element at the end of L
void prepend(List L, int data) {
if(L == NULL){
printf("List Error: calling prepend() on NULL List reference\n");
exit(1);
}
if(isEmpty(L)) {
Node N = newNode(data);
L->front = L->back = N;
}else{
Node N = newNode(data);
L->front->previous = N;
N->next = L->front;
L->front = N;
}
L->length++;
if(index(L) >= 0) {
L->index++;
}
}
// insertBefore()
// Insert new element before cursor.
// Pre: length()>0, index()>=0
void insertBefore(List L, int data) {
if(isEmpty(L)) {
printf("List Error: insertBefore() called on empty List\n");
}
if(L->cursor == NULL) {
printf("List Error: inserBefore() called on undefined cursor\n");
}
if (index(L) == 0) {
prepend(L, data);
} else {
Node N = newNode(data);
N->next = L->cursor;
N->previous = L->cursor->previous;
L->cursor->previous->next = N;
L->cursor->previous = N;
L->index++;
L->length++;
}
}
// insertAffer()
// Insert new element After cursor.
// Pre: length()>0, index()>=0
void insertAfter(List L, int data) {
if(isEmpty(L)) {
printf("List Error: insertAfter() called on empty List\n");
}
if(L->cursor == NULL) {
printf("List Error: insertAfter() called on undefined cursor\n");
}
if (index(L) == length(L) - 1) {
append(L, data);
} else {
Node N = newNode(data);
N->next = L->cursor->next;
L->cursor->next->previous = N;
N->previous = L->cursor;
L->cursor->next = N;
L->length++;
}
}
// delete()
// Deletes element at cursor position
// Pre: !isEmpty(L) and cursor is defined
void delete(List L) {
if(isEmpty(L)) {
printf("List Error: delete() called on empty List\n");
}
if(L->cursor == NULL) {
printf("List Error: delete() called on undefined cursor\n");
}
if(index(L) == 0) {
deleteFront(L);
} else if(index(L) == length(L) - 1) {
deleteBack(L);
} else {
L->cursor->previous->next = L->cursor->next;
L->cursor->next->previous = L->cursor->previous;
freeNode(&L->cursor); //change this to L->cursor maybe.
L->cursor = NULL;
L->index= -1;
L->length--;
}
}
// deleteBack()
// Deletes element at back of L
// Pre: !isEmpty(L)
void deleteBack(List L) {
Node N = NULL;
if(L == NULL) {
printf("List Error: calling deleteFront() on NULL List reference\n");
exit(1);
}
if(isEmpty(L)){
printf("List Error: calling deleteFront on an empty List\n");
exit(1);
}
N = L->back;
if(length(L) > 1) {
L->back = L->back->previous;
} else {
L->back = L->front = NULL;
}
L->length--;
freeNode(&N);
}
// deletefront()
// Deletes element at front of L
// Pre: !isEmpty(L)
void deleteFront(List L) {
Node N = NULL;
if(L == NULL) {
printf("List Error: calling deleteFront() on NULL List reference\n");
exit(1);
}
if(isEmpty(L)){
printf("List Error: calling deleteFront on an empty List\n");
exit(1);
}
N = L->front;
if(length(L) > 1) {
L->front = L->front->next;
}else{
L->front = L->back = L->cursor = NULL;
L->index = -1;
}
L->length--;
if(index(L) >= 0) {
L->index--;
}
freeNode(&N);
}
// clear()
// resets list to it's original empty state.
void clear(List L) {
moveFront(L);
while(L->cursor != NULL) {
moveNext(L);
deleteFront(L);
}
}
// Other Functions ------------------------------------------------------------
// printList()
// Prints data elements in L on a single line to stdout.
void printList(FILE* out, List L){
Node N = NULL;
if(L==NULL){
printf("List Error: calling printList() on NULL List reference\n");
exit(1);
}
for(N = L->front; N != NULL; N = N->next){
fprintf(out, "%d ", N->data);
}
//printf("\n");
}
// copyList()
// Returns a new List representing the same integer sequence as this
// List. The cursor in the new list is undefined, regardless of the
// state of the cursor in this List. This List is unchanged.
List copyList(List L) {
List Q = newList();
int spot = index(L);
for(moveFront(L); index(L) >= 0; moveNext(L)){
append(Q, get(L));
}
if(spot >= 0) {
moveFront(L);
while(index(L) != spot) {
moveNext(L);
}
}
return Q;
}