-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.java
739 lines (584 loc) · 14.5 KB
/
List.java
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import java.io.*;
public class List extends Base {
static boolean debug = false; // debug status
static int listCounter = 0; // used to number each List
public static final int // List controls
END = 0,
FRONT = 1,
SORTED = 2;
private class ListEngine {
// catastrophic error messages
static final String
ADNEXT_EMPTY = "Advance next from empty list!!!\n",
ADPRE_EMPTY = "Advance pre from empty list!!!\n",
REMOVE_EMPTY = "Remove from empty list!!!\n",
VIEW_EMPTY = "Viewing an empty list!!!\n",
WRITE_NONEXISTFILE
= "Writing to a non-existent file!!!\n";
// debug messages
static final String
ADNEXT = "[List %d - Advancing next]\n",
ADPRE = "[List %d - Advancing pre]\n",
INSERT = "[List %d - Inserting node]\n",
LOOKUP = "[List %d - Looking up node]\n",
REMOVE = "[List %d - Removing node]\n",
VIEW = "[List %d - Viewing node]\n",
LIST_ALLOCATE
= "[List %d has been allocated]\n",
LIST_JETTISON
= "[List %d has been jettisoned]\n";
int count; // which list is it
Node front; // start of the List
long occupancy; // how many items stored
Base sample; // sample object of what is stored
Tracker tracker; // to track memory
// TODO: ListEngine CTOR METHOD HEADER
ListEngine (Base sample, String caller) {
tracker = new Tracker ("ListEngine",
Size.of (count)
+ Size.of (front)
+ Size.of (occupancy)
+ Size.of (sample)
+ Size.of (tracker),
caller + " calling ListEngine Ctor");
// ---- DO NOT CHANGE TRACKER ---- //
// TODO: YOUR CODE GOES HERE
front = null;
occupancy = 0;
listCounter += 1;
count = listCounter;
this.sample = sample;
if (debug) {
System.err.print (
String.format (
LIST_ALLOCATE,
count));
}
}
// TODO: ListEngine JETTISION METHOD HEADER
void jettisonList () {
// TODO: YOUR CODE GOES HERE
if (debug) {
System.err.print (
String.format (
LIST_JETTISON,
count));
}
for (int idx = 0; idx < occupancy - 1; idx++) {
Node oldFront = front;
front = front.getNext ();
oldFront.setNext (null);
front.setPre (null);
oldFront.jettisonNode ();
}
if (front != null) {
front.jettisonNode ();
}
tracker.jettison ();
front = null;
listCounter--;
count = listCounter;
}
// TODO: ListEngine ISEMPTY METHOD HEADER
boolean isEmpty () {
// TODO: YOUR CODE GOES HERE
return (occupancy == 0);
}
// TODO: ListEngine ADVANCENEXT METHOD HEADER
void advanceNext () {
// TODO: YOUR CODE GOES HERE
if (debug) {
System.err.print (
String.format (
ADNEXT, count));
}
if (isEmpty ()) {
System.err.print (ADNEXT_EMPTY);
return;
}
front = front.getNext ();
}
// TODO: ListEngine ADVANCEPRE METHOD HEADER
void advancePre () {
// TODO: YOUR CODE GOES HERE
if (debug) {
System.err.print (
String.format (
ADNEXT, count));
}
if (isEmpty ()) {
System.err.print (ADNEXT_EMPTY);
return;
}
front = front.getPre ();
}
// TODO: ListEngine INSERT METHOD HEADER
boolean insert (Base element, long where) {
// TODO: YOUR CODE GOES HERE
if (debug) {
System.err.print (
String.format (
INSERT, count));
}
if (isEmpty ()) {
front = new Node (element);
return true;
}
Node frontPtr = front;
// locate
if (where == SORTED) {
if (locate (element)) {
where = FRONT;
}
}
front = front.insert (element);
if (where != FRONT) { // insert not at front
front = frontPtr;
}
return true;
}
// TODO: ListEngine LOCATE METHOD HEADER
boolean locate (Base element) {
// TODO: YOUR CODE GOES HERE
int idx = 0;
for (idx = 0; idx < occupancy; idx ++) {
if (element.isLessThan (
front.nodeEngine.data)) {
break;
}
else {
advanceNext ();
}
}
return idx == 0;
}
// TODO: ListEngine REMOVE METHOD HEADER
Base remove (long where) {
// TODO: YOUR CODE GOES HERE
if (debug) {
System.err.print (
String.format (
REMOVE, count));
}
if (isEmpty ()) {
System.err.print (REMOVE_EMPTY);
return null;
}
Node frontPtr = front;
if (where == FRONT) {
frontPtr = front.getNext ();
}
else {
advancePre ();
}
Base ret = front.remove ();
occupancy --;
front = frontPtr;
if (occupancy == 0) {
front = null;
}
return ret;
}
// TODO: ListEngine VIEW METHOD HEADER
Base view (long where) {
// TODO: YOUR CODE GOES HERE
Node nodeToView = front;
if (debug) {
System.err.print (
String.format (
VIEW, count));
}
if (listEngine.isEmpty ()) {
System.err.print (VIEW_EMPTY);
return null;
}
if (where == END) {
nodeToView = nodeToView.getPre ();
}
return nodeToView.view ();
}
// ListEngine WRITELIST
void writeList (PrintStream stream) {
if (stream == null) {
System.err.print (WRITE_NONEXISTFILE);
return;
}
// extra output if we are debugging
if (stream == System.err) {
stream.print ("List "
+ count + " has "
+ occupancy + " items in it\n");
}
// display each Node in the List
Node priorFront = front; // to save prior front
for (long idx = 1; idx <= occupancy; idx++) {
stream.print (" element " + idx + ": ");
front.writeNode (stream);
advanceNext ();
}
// memory tracking output if we are debugging
if (debug) {
System.err.print (tracker);
}
// restore front to prior value
front = priorFront;
}
// TODO: ListEngine WRITEREVERSELIST METHOD HEADER
void writeReverseList (PrintStream stream) {
// TODO: YOUR CODE GOES HERE
if (stream == null) {
System.err.print (WRITE_NONEXISTFILE);
return;
}
Node frontTemp = front;
if (stream == System.err) {
stream.print ("List " + count
+ " has "+ occupancy
+ " items in it\n");
}
for (long idx = 1; idx <= occupancy; idx++) {
advancePre ();
stream.print (" element " + idx + ": ");
front.writeNode (stream);
}
if (debug) {
System.err.print (tracker);
}
front = frontTemp;
}
// List TOSTRING METHOD
public String toString () {
long count; /* to know how many elements to print */
Node current; /* working node */
String string = ""; /* string to be returned */
if(isEmpty ())
return "empty";
current = front;
for (count = 1; count <= occupancy; count++) {
string += "" + current;
current = current.getNext ();
}
return string;
}
private class Node {
private class NodeEngine {
static final String WRITE_NONEXISTFILE
= "Writing to a "
+ "non-existent file!!!\n";
Base data; // the item stored
Node next; // to get to following item
Node pre; // to get to previous item
Tracker tracker; // to track memory
// TODO: NodeEngine CTOR METHOD HEADER HERE.
NodeEngine (Node newNode,
Base element, String caller) {
tracker = new Tracker ("NodeEngine",
Size.of (data)
+ Size.of (next)
+ Size.of (pre)
+ Size.of (tracker),
caller
+= " calling NodeEngine Ctor");
// ---- DO NOT CHANGE TRACKER ---- //
// TODO: YOUR CODE GOES HERE
data = (sample == null) ? element
: sample.copy ();
// In a circular linked list, never null
pre = newNode;
next = newNode;
occupancy++;
}
// TODO: NodeEngine JETTISON METHOD HEADER HERE.
void jettisonNode () {
// TODO: YOUR CODE GOES HERE
tracker.jettison ();
data.jettison();
data = null;
}
// TODO: NodeEngine JETTISON METHOD HEADER HERE.
void jettisonNodeOnly () {
// TODO: YOUR CODE GOES HERE
tracker.jettison ();
data = null;
}
// TODO: NodeEngine INSERT METHOD HEADER HERE.
Node insert (Node frontNode,
Base element) {
// TODO: YOUR CODE GOES HERE
Node newNode = new Node (element);
// attach
newNode.setPre (pre);
newNode.setNext (frontNode);
// integrate
pre.setNext (newNode);
pre = newNode;
return newNode;
}
// TODO: NodeEngine REMOVE METHOD HEADER HERE.
Base remove () {
// TODO: YOUR CODE GOES HERE
Base ret = data;
boolean nodeOnly = true;
// restucture the list
pre.setNext (next);
next.setPre (pre);
jettisonNodeOnly ();
return ret;
}
// TODO: NodeEngine VIEW METHOD HEADER HERE.
Base view () {
// TODO: YOUR CODE GOES HERE
return data;
}
// NodeEngineWRITENODE METHOD
void writeNode (PrintStream stream) {
if (stream == null) {
System.err.print (
WRITE_NONEXISTFILE);
return;
}
stream.print (data + "\n");
}
}
// -------- YOUR CODE SHOULD GO ABOVE --------
// NOTE:
// READ THE CODE BELOW TO SEE WHAT METHOD YOU CAN USE
static final String
GETPRE_NONEXISTNODE
= "Getting pre of a non-existent node!!!\n",
GETNEXT_NONEXISTNODE
= "Getting next of a non-existent node!!!\n",
SETPRE_NONEXISTNODE
= "Setting pre of a non-existent node!!!\n",
SETNEXT_NONEXISTNODE
= "Setting next of a non-existent node!!!\n",
JETTISON_NONEXISTNODE
= "Jettisoning a non-existent node!!!\n",
LOOKUP_NONEXISTNODE
= "Looking up a non-existent node!!!\n",
INSERT_NONEXISTNODE
= "Inserting a non-existent node!!!\n",
REMOVE_NONEXISTNODE
= "Removing a non-existent node!!!\n",
VIEW_NONEXISTNODE
= "Viewing a non-existent node!!!\n",
WRITE_NONEXISTNODE
= "Writing from a non-existent node!!!\n";
NodeEngine nodeEngine;
// Node CTOR METHOD
Node (Base element) {
nodeEngine = new NodeEngine (
this, element, "Node Ctor");
}
Base getData () {
return nodeEngine.data;
}
// Node GETPRE METHOD
Node getPre () {
if (!exist ()) {
System.err.print (
GETPRE_NONEXISTNODE);
return null;
}
return nodeEngine.pre;
}
// Node GETNEXT METHOD
Node getNext () {
if (!exist ()) {
System.err.print (
GETNEXT_NONEXISTNODE);
return null;
}
return nodeEngine.next;
}
// Node SETNEXT METHOD
void setNext (Node next) {
if (!exist ()) {
System.err.print (
SETNEXT_NONEXISTNODE);
return;
}
nodeEngine.next = next;
}
void setPre (Node pre) {
if (!exist ()) {
System.err.print (
SETPRE_NONEXISTNODE);
return;
}
nodeEngine.pre = pre;
}
// Node JETTISON METHOD
boolean jettisonNode () {
if (!exist ()) {
System.err.print (
JETTISON_NONEXISTNODE);
return false;
}
nodeEngine.jettisonNode ();
nodeEngine = null;
return true;
}
// Node EXIST METHOD
boolean exist () {
return nodeEngine != null;
}
// Node INSERT METHOD
Node insert (Base element) {
if (!exist ()) {
System.err.print (INSERT_NONEXISTNODE);
return null;
}
return nodeEngine.insert (this, element);
}
// Node REMOVE METHOD
Base remove () {
if (!exist ()) {
System.err.print (REMOVE_NONEXISTNODE);
return null;
}
return nodeEngine.remove ();
}
// Node VIEW METHOD
Base view () {
if (!exist ()) {
System.err.print (
VIEW_NONEXISTNODE);
return null;
}
return nodeEngine.view ();
}
// Node WRITENODE METHOD
void writeNode (PrintStream stream) {
nodeEngine.writeNode (stream);
}
public String toString () {
return "" + getData ();
}
}
}
// catastrophic error messages
static final String
ADNEXT_NONEXIST = "Advance next from non-existent list!!!\n",
ADPRE_NONEXIST = "Advance pre from non-existent list!!!\n",
JETTISON_NONEXIST = "Jettisoning from non-existent list!!!\n",
EMPTY_NONEXIST = "Empyting from non-existent list!!!\n",
ISEMPTY_NONEXIST = "Is empty check from non-existent list!!!\n",
INSERT_NONEXIST = "Inserting to a non-existent list!!!\n",
REMOVE_NONEXIST = "Removing from non-existent list!!!\n",
VIEW_NONEXIST = "Viewing a non-existent list!!!\n",
WRITE_NONEXISTLIST = "Writing from a non-existent list!!!\n",
WRITE_MISSINGFUNC = "Don't know how to write out elements!!!\n";
private ListEngine listEngine; // The ListEngine instance
public static void debugOn () {
debug = true;
}
public static void debugOff () {
debug = false;
}
// List CTOR METHOD
public List (Base sample, String caller) {
caller += " calling List Ctor";
listEngine = new ListEngine (sample, caller);
}
// list JETTISON
public void jettison () {
jettisonList ();
}
// list JETTISON
public boolean jettisonList () {
if (!exist ()) {
System.err.print (JETTISON_NONEXIST);
return false;
}
listEngine.jettisonList ();
listEngine = null;
return true;
}
// List ADVANCENPRE METHOD
public void advancePre () {
if (!exist ()) {
System.err.print (ADPRE_NONEXIST);
return;
}
listEngine.advancePre ();
}
// List ADVANCENEXT METHOD
public void advanceNext () {
if (!exist ()) {
System.err.print (ADNEXT_NONEXIST);
return;
}
listEngine.advanceNext ();
}
// List EMPTY METHOD
public void empty () {
if (!exist ()) {
System.err.print (EMPTY_NONEXIST);
return;
}
while (!isEmpty ()) {
listEngine.remove (0);
}
}
// List EXIST METHOD
public boolean exist () {
return listEngine != null;
}
// List GETOCCUPANCY METHOD
public long getOccupancy () {
return listEngine.occupancy;
}
// List ISEMPTY METHOD
public boolean isEmpty () {
if (!exist ()) {
System.err.print (ISEMPTY_NONEXIST);
return false;
}
return listEngine.isEmpty ();
}
// List INSERT METHOD
public boolean insert (Base element, long where) {
if (!exist ()) {
System.err.print (INSERT_NONEXIST);
return false;
}
return listEngine.insert (element, where);
}
// List REMOVE METHOD
public Base remove (long where) {
if (!exist ()) {
System.err.print (REMOVE_NONEXIST);
return null;
}
return listEngine.remove (where);
}
// List TOSTRING METHOD
public String toString () {
return listEngine.toString ();
}
// List VIEW METHOD
public Base view (long where) {
if (!exist ()) {
System.err.print (VIEW_NONEXIST);
return null;
}
return listEngine.view (where);
}
// List WRITELIST METHOD
public void writeList (PrintStream stream) {
if (!exist ()) {
System.err.print (WRITE_NONEXISTLIST);
return;
}
listEngine.writeList (stream);
}
// List WRITEREVERSELIST METHOD
public void writeReverseList (PrintStream stream) {
if (!exist ()) {
System.err.print (WRITE_NONEXISTLIST);
return;
}
listEngine.writeReverseList (stream);
}
}