-
Notifications
You must be signed in to change notification settings - Fork 2
/
List.c
520 lines (466 loc) · 10.8 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**********************************************************************************
*
* Raj Maitra
* Rmaitra
* pa5
*
***********************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include "List.h"
/************** Private Structs: not exported *************************************/
typedef struct Node{
int data;
struct Node* next;
struct Node* previous;
} Node;
typedef Node* NodeRef;
typedef struct List{
NodeRef front;
NodeRef back;
NodeRef current;
int length;
int index;
} List;
/************** Constructors-Destructors ******************************************/
/*
* newNode
* Returns pointer to new Node struct. Initializes next field to NULL, and sets
* data field to input parameter node_data. Private.
*/
NodeRef newNode(int node_data){
NodeRef N = malloc(sizeof(Node));
N->data = node_data;
N->next = NULL;
N->previous = NULL;
return(N);
}
/*
* freeNode
* Frees heap memory pointed to by pN. Private.
*/
void freeNode(NodeRef* pN){
if( pN!=NULL && *pN!=NULL ){
free(*pN);
*pN = NULL;
}
}
/*
* newList
* Returns ListRef pointing to new ListStruct which represents an empty List.
* Initializes front and back fields to NULL, sets length field to 0. Exported.
*/
ListRef newList(void){
ListRef L = malloc(sizeof(List));
L->front = NULL;
L->back = NULL;
L->current = NULL;
L->length = 0;
L->index = -1;
return(L);
}
/*
* freeList
* Frees all heap memory associated with the ListRef *pL, including all memory
* in existing Nodes. Sets *pL to NULL. Exported.
*/
void freeList(ListRef* pL){
if(pL==NULL || *pL==NULL) { return; }
while( !isEmpty(*pL) ) { deleteBack(*pL); }
free(*pL);
*pL = NULL;
}
/***************** Access functions ***********************************************/
/* getFront */
int getFront(ListRef L){
if( L==NULL ){
printf("List Error: calling getFront() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling getFront() on an empty List\n");
exit(1);
}
return(L->front->data);
}
/*
* getBack
* Returns the value at the front of L.
* Pre: !isEmpty(Q)
*/
int getBack(ListRef L){
if( L==NULL ){
printf("List Error: calling getBack() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling getBack() on an empty List\n");
exit(1);
}
return(L->back->data);
}
/*
* getCurrent
* Returns the value at the front of L.
* Pre: !isEmpty(L)
*/
int getCurrent(ListRef L){
if( L==NULL ){
printf("List Error: calling getCurrent() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling getCurrent() on an empty List\n");
exit(1);
}
if( offEnd(L) ){
printf("List Error: calling getCurrent() when off the end of the List\n");
exit(1);
}
return(L->current->data);
}
/*
* getLength
* Returns the length of Q
*/
int getLength(ListRef L){
if( L==NULL ){
printf("List Error: calling getLength() on NULL ListRef\n");
exit(1);
}
return(L->length);
}
/*
* getIndex
* Returns the length of Q
*/
int getIndex(ListRef L){
if( L==NULL ){
printf("List Error: calling getIndex() on NULL ListRef\n");
exit(1);
}
return(L->index);
}
/*
* isEmpty
* Returns True if Q is empty, otherwise returns 0
*/
int isEmpty(ListRef L){
if( L==NULL ){
printf("List Error: calling isEmpty() on NULL ListRef\n");
exit(1);
}
return(L->length==0);
}
int offEnd(ListRef L){
if( L->index==-1 ){
printf("List Error: calling offEnd() when not on the List\n");
exit(1);
}
return(0);
}
/**************** Manipulation procedures ****************************************/
/* makeEmpty */
void makeEmpty(ListRef L){
while(L->length > 0){
deleteBack(L);
}
}
/* moveTo */
void moveTo(ListRef L, int i){
int j;
if( isEmpty(L) ){
printf("List Error: calling moveNext() on Empty List\n");
exit(1);
}
if(i == 0){
L->current = L->front;
L->index = 0;
}
if(i == L->length-1){
L->current = L->back;
L->index = 0;
}
else{
L->current = L->front;
L->index = 0;
for(j = 0; j<i; j++){
moveNext(L);
}
}
}
/* movePrev */
void movePrev(ListRef L){
if( L==NULL ){
printf("List Error: calling moveNext() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling moveNext() on Empty List\n");
exit(1);
}
if( L->index==-1 || L->index<=0){
printf("List Error: calling offEnd() when not on the List\n");
exit(1);
}
L->current = L->current->previous;
L->index--;
}
/* moveNext */
void moveNext(ListRef L){
if( L==NULL ){
printf("List Error: calling moveNext() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling moveNext() on Empty List\n");
exit(1);
}
if( L->index ==-1 || L->index >= L->length){
printf("List Error: calling offEnd() when not on the List\n");
exit(1);
}
L->current = L->current->next;
L->index++;
}
/* insertFront */
void insertFront(ListRef L, int data){
if( L==NULL ){
printf("List Error: calling insertFront() on NULL ListRef\n");
exit(1);
}
NodeRef N = newNode(data);
if( isEmpty(L) ){
L->front = L->back = N;
L->current = N;
L->index = 0;
}
else{
N->next = L->front;
L->front->previous = N;
L->front = N;
L->current = L->front;
L->index = 0;
}
L->length++;
}
/* insert Back*/
void insertBack(ListRef L, int data){
if( L==NULL ){
printf("List Error: calling insertBack() on NULL ListRef\n");
exit(1);
}
NodeRef N = newNode(data);
if( isEmpty(L) ){
L->front = L->back = N;
L->current = N;
L->index = 0;
}
else{
N->previous = L->back;
L->back->next = N;
L->back = N;
L->current = L->back;
L->index = L->length-1;
}
L->length++;
}
/*/insertBeforeCurrent*/
void insertBeforeCurrent(ListRef L, int data){
if( L==NULL ){
printf("List Error: calling insertBeforeCurrent() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling insertBeforeCurrent() on an Empty List\n");
exit(1);
}
if( getIndex(L) ==0){
insertFront(L, data);
}
else{
NodeRef N = newNode(data);
N->next = L->current;
N->previous = L->current->previous;
L->current->previous->next = N;
L->current->previous = N;
L->current = L->current->previous;
L->current->next = N->next;
L->current->previous = N->previous;
L->length++;
}
}
/*/insertAfterCurrent*/
void insertAfterCurrent(ListRef L, int data){
if( L==NULL ){
printf("List Error: calling insertAfterCurrent() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling insertAfterCurrent() on an Empty List\n");
exit(1);
}
if( getIndex(L)==getLength(L)-1){
insertBack(L, data);
}
else{
NodeRef N = newNode(data);
N->previous = L->current;
N->next = L->current->next;
L->current->next->previous = N;
L->current->next = N;
L->current = L->current->next;
L->current->next = N->next;
L->current->previous = N->previous;
L->index++;
}
}
/* deleteFront */
void deleteFront(ListRef L){
NodeRef N = NULL;
if( L==NULL ){
printf("List Error: calling deleteFront() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling deleteFront() on an empty List\n");
exit(1);
}
N = L->front;
if( getLength(L)>1 ){
L->front = L->front->next;
L->front->previous = NULL;
L->current = L->front;
}
else {
L->front = L->back = NULL;
L->current = NULL;
}
L->length--;
freeNode(&N);
}
/* deleteBack */
void deleteBack(ListRef L){
NodeRef N = NULL;
if( L==NULL ){
printf("List Error: calling deleteBack() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling deleteBack() on an empty List\n");
exit(1);
}
N = L->back;
if( getLength(L)>1 ){
L->back = L->back->previous;
L->back->next = NULL;
L->current = L->back;
}
else {
L->front = L->back = NULL;
L->current = NULL;
}
L->length--;
freeNode(&N);
}
/* deleteCurrent */
void deleteCurrent(ListRef L){
NodeRef N = NULL;
if( L==NULL ){
printf("List Error: calling deleteCurrent() on NULL ListRef\n");
exit(1);
}
if( isEmpty(L) ){
printf("List Error: calling deleteCurrent() on an empty List\n");
exit(1);
}
if(L->index == L->length-1){
deleteBack(L);
}
else if(L->index == 0){
deleteFront(L);
}
else if( getLength(L)>1 ){
N = L->current;
L->current->next->previous = L->current->previous;
L->current->previous->next = L->current->next;
L->current = NULL;
L->length--;
}
else {
N = L->current;
L->front = L->back = NULL;
L->current = NULL;
L->length--;
}
L->index = -1;
freeNode(&N);
}
/*************** Other Functions *************************************************/
/*
* printList
* Prints data elements in L on a single line to stdout.
*/
void printList(FILE *out, ListRef L){
int i;
if( L==NULL ){
printf("List Error: calling printList() on NULL ListRef\n");
exit(1);
}
for(i = 1; i<=getLength(L); i++){
moveTo(L, i-1);
fprintf(out, "%d", getCurrent(L));
if(i<getLength(L)){
fprintf(out, " ");
}
}
}
/*
* equals
* returns 1 if A is identical to B, 0 otherwise
*/
int equals(ListRef A, ListRef B){
int flag = 1;
Node* N = NULL;
Node* M = NULL;
if( A==NULL || B==NULL ){
printf("List Error: calling equals() on NULL ListRef\n");
exit(1);
}
N = A->front;
M = B->front;
if( A->length != B->length ) { return 0; }
while( flag && N!=NULL){
flag = (N->data==M->data);
N = N->next;
M = M->next;
}
return flag;
}
/* copyList */
ListRef copyList(ListRef L){
ListRef X = newList();
int i;
if( L == NULL){
printf("List Error: calling printList() on NULL ListRef\n");
exit(1);
}
moveTo(L, 0);
insertBack(X, getCurrent(L));
for(i = 1; i<L->length; i++){
moveNext(L);
insertBack(X, getCurrent(L));
}
return(X);
}
void printflist(ListRef L){
Node* N = NULL;
if( L==NULL ){
printf("Queue Error: calling printQueue() on NULL QueueRef\n");
exit(1);
}
for(N = L->front; N != NULL; N = N->next){
printf("%d ", N->data);
}
printf("\n");
}