-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleLinkedListX.java
1081 lines (883 loc) · 30.6 KB
/
DoubleLinkedListX.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package asdlTugas2;
// Created by Sriwijaya University //
// Billingual Class A 2021 //
// Group 1 //
// Advanced Algorithm & Data Structure //
import java.util.*;
public class DoubleLinkedListX {
// Represents a Node of the Double Linked List
Node head;
Node tail;
int size;
static class Node {
int data;
Node prev;
Node next;
Node(int d) {
data = d;
prev = null;
next = null;
}
}
public int size() {
// Returns the size of the Double Linked List
return size;
}
public void printList(boolean asc) { // Prints the Double Linked List data
Node nodes = null;
if (asc)
// Starts from the head
nodes = head;
else
// Starts from the tail
nodes = tail;
System.out.println("#### \tDOUBLE LINKED LIST PRINTED DATA\t####");
System.out.print("\t[ ");
while (nodes != null) {
// Prints the data of the node in the list
System.out.print(nodes.data + " ");
if (asc)
// Cycles through the head to tail or vice versa
nodes = nodes.next;
else
nodes = nodes.prev;
}
System.out.println("]");
}
public void addFirst(int data) { // Adds data from the first node of the list
// Create a new Node
Node newNode = new Node(data);
if (head == null) { // If the list is empty do:
// Both head and tail will point to the new Node
head = tail = newNode;
// Head (newNode) previous is null
head.prev = null;
// Tail (newNode) next is null, as it is the last Node
tail.next = null;
} else {
// Adds a new node before the head and makes it the head
newNode.next = head;
newNode.prev = null;
head.prev = newNode;
head = newNode;
}
// Increases the size of list
size++;
}
public void addMiddle(int data) { // Adds the data from the middle of the list
if (size < 3) { // Checks if the list is large enough to avoid index larger than list
System.out.println("The list is not large enough, please use 'Add First Data' first.");
return;
}
// Creates a new Node
Node newNode = new Node(data);
if (head == null) { // If the list is empty do:
// Both head and tail will point to the new Node
head = tail = newNode;
// Head (newNode) previous is null
head.prev = null;
// Tail (newNode) next is null
tail.next = null;
} else {
// Starts the current at head
Node current = head, temp = null;
// Finds the middle point of the List
int mid = (size % 2 == 0) ? (size / 2) : ((size + 1) / 2);
// Iterates through the list until the middle point
for (int i = 0; i < mid; i++) {
current = current.next;
}
// Node temp points to the Node next to the current
temp = current.next;
temp.prev = current;
// New node added between the current and temp
current.next = newNode;
newNode.prev = current;
newNode.next = temp;
temp.prev = newNode;
}
size++;
}
public void addLast(int data) { // Adds the data from the last node of the list
// Starts from the head
Node last = head;
// Creates a new Node
Node node = new Node(data);
if (head == null) { // If the list is empty do:
// Both head and tail will point to the new Node
head = tail = node;
// Head (newNode) previous is null
head.prev = null;
// Tail (newNode) next is null
tail.next = null;
} else {
// Iterates until the last Node
while (last.next != null) {
last = last.next;
}
// Adds a new Node next to the last Node and makes it the tail
last.next = node;
node.prev = last;
last = node;
tail = last;
}
size++;
}
public void removeFirst() {
// If list is empty, delete nothing
if (head == null) {
System.out.println("List is empty, please add data first");
return;
}
// Deletes the first node and makes the next node the head
Node node = head.next;
node.prev = null;
head = node;
// Reduces the List size
size--;
}
public void removeMid() {
// If list is empty, delete nothing
if (head == null) {
System.out.println("List is empty, please add data first");
return;
}
// Starts from the head
Node current = head;
// Find the middle point of the list
int mid = (size % 2 == 0) ? (size / 2) : ((size + 1) / 2);
// Iterates until the middle point of the list
for (int i = 0; i < mid; i++) {
current = current.next;
}
if (current == head) { // If the middle node is the head, do:
// Moves the head to the next node
head = current.next;
} else if (current == tail) { // If the middle node is the tail, do:
// Moves the tail to the previous node
tail = current.prev;
} else {
// Removes the current from the next node and previous node connections
current.prev.next = current.next;
current.next.prev = current.prev;
}
// Nullifies the current node
current = null;
size--;
}
public void removeLast() {
// If list is empty, delete nothing
if (head == null) {
System.out.println("List is empty, please add data first");
return;
}
// Removes the last node and makes the previous node the tail
Node node = tail.prev;
node.next = null;
tail = node;
size--;
}
public boolean find(int data) {
// Starts the flag that is false until data is found
boolean flag = false;
// Starts from the head
Node n = head;
// Iterates through the list
while (n != null) {
if (n.data == data) { // If the data is in the list, do:
// Makes the flag true
flag = true;
// Breaks the iteration
break;
}
// Continue iterating to the next
n = n.next;
}
// Returns the flag
return flag;
}
public boolean replace(int number, int data) {
// Starts from the head
Node nodes = head;
boolean flag = false;
// Iterates through the list
while (nodes != null) {
if (nodes.data == number) { // If the data is found in the list, do:
flag = true;
// Replaces the nodes data to the new data
nodes.data = data;
// Breaks the iteration
break;
}
// Continue iterating to the next
nodes = nodes.next;
}
return flag;
}
public int sum() {
// Starts from the head
Node nodes = head;
// Starts the sum value
int sum = 0;
// Iterates through the list
while (nodes != null) {
// Sums all the nodes data in the list
sum += nodes.data;
// Continue iterating to the next
nodes = nodes.next;
}
// Returns the sum of list data
return sum;
}
public double average() {
// Starts from the head
Node nodes = head;
// Starts the sum and average value
int sum = 0;
double average = 0;
// Iterates through the list
while (nodes != null) {
// Sums all the nodes data in the list
sum += nodes.data;
// Continue iterating to the next
nodes = nodes.next;
}
// Checks if the list is empty to avoid division by zero
if (size == 0)
average = sum / 1;
else
// Divides the sum by the list size
average = (double) sum / (double) size;
// Returns the average value
return average;
}
public int median() {
// Starts from the head
Node nodes = head;
// If list is empty, return 0
if (head == null) {
return 0;
}
// Find the middle point of the list
int mid = (size % 2 == 0) ? (size / 2) : ((size + 1) / 2);
// Iterates until the middle point of the list
for (int i = 0; i < mid; i++) {
nodes = nodes.next;
}
// Returns the middle nodes data
return nodes.data;
}
public int max() {
// Starts from the head
Node nodes = head;
// Starts the max value
int max = 0;
// Iterates through the list
while (nodes != null) {
if (nodes.data > max) { // Finds the largest number in the list
max = nodes.data;
}
// Continue iterating to the next
nodes = nodes.next;
}
// Returns the max value
return max;
}
public int min() {
// Starts from the head
Node nodes = head;
// Starts the min value
int min = 0;
// Iterates through the list
while (nodes != null) {
if (nodes.data < min) { // Finds the smallest number in the list
min = nodes.data;
}
// Continue iterating to the next
nodes = nodes.next;
}
// Returns the min value
return min;
}
public double variance() {
// If list is empty, return 0
if (head == null) {
return 0;
}
// Starts from the head
Node nodes = head;
// Starts the sum and average value
int sum = 0;
double average = 0;
double variance = 0;
// Iterates through the list
while (nodes != null) {
// Sums all the nodes data in the list
sum += nodes.data;
// Continue iterating to the next
nodes = nodes.next;
}
// Checks if the list is empty to avoid division by zero
if (size == 0)
average = sum / 1;
else
// Divides the sum by the list size
average = (double) sum / (double) size;
// Starts at the head again
nodes = head;
// Restarts the sum value
sum = 0;
// Iterates through the list
while (nodes != null) {
// Sums up all the nodes data minus the average, and powered by 2
sum += Math.pow(nodes.data - average, 2);
// Continue iterating to the next
nodes = nodes.next;
}
if (size <= 1) // If the size is smaller or equal to 1, divide the sum by 1
variance = sum / 1;
else // Divides the sum by the list size minus 1
variance = sum / size - 1;
// Returns the variance value
return variance;
}
public double standardDeviation() {
// If list is empty, return 0
if (head == null) {
return 0;
}
// Starts from the head
Node nodes = head;
// Starts the sum and average value
int sum = 0;
double average = 0;
double variance = 0;
// Iterates through the list
while (nodes != null) {
// Sums all the nodes data in the list
sum += nodes.data;
// Continue iterating to the next
nodes = nodes.next;
}
// Checks if the list is empty to avoid division by zero
if (size == 0)
average = sum / 1;
else
// Divides the sum by the list size
average = (double) sum / (double) size;
// Starts at the head again
nodes = head;
// Restarts the sum value
sum = 0;
// Iterates through the list
while (nodes != null) {
// Sums up all the nodes data minus the average, and powered by 2
sum += Math.pow(nodes.data - average, 2);
// Continue iterating to the next
nodes = nodes.next;
}
if (size <= 1) // If the size is smaller or equal to 1, divide the sum by 1
variance = sum / 1;
else // Divides the sum by the list size minus 1
variance = sum / size - 1;
// Returns the standard deviation value
return Math.sqrt(variance);
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// Starts the Scanner, Double Linked List, integers, boolean, and input
Scanner scan = new Scanner(System.in);
DoubleLinkedListX dllist = new DoubleLinkedListX();
int numbers = 0;
int index = 1;
boolean asc = true;
String input = "0";
while (true) { // MAIN MENU
System.out.println("#### \tDOUBLE LINKED LIST\t####");
System.out.println("\t1. Manual" + "\n\t2. Packaged" + "\n\t3. EXIT");
System.out.print("\tPlease choose [1/2/3]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // MANUAL MENU
while (true) {
System.out.println("#### \tMANUAL OPERATIONS\t####");
System.out.println("\t1. Add Data" + "\n\t2. Remove Data" + "\n\t3. Replace/Find Data"
+ "\n\t4. Statistics" + "\n\t5. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // DATA ADDITION MENU
while (true) {
System.out.println("#### \tDATA ADDITION OPERATIONS\t####");
System.out.println("\t1. Add First Data" + "\n\t2. Add Middle Data" + "\n\t3. Add Last Data"
+ "\n\t4. Print Data" + "\n\t5. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // FIRST DATA ADDITION MENU
index = 1;
System.out.println("#### \tFIRST DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("\tData " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enters the input to the first node of the list
numbers = scan.nextInt();
dllist.addFirst(numbers);
index++;
System.out.print("\tData " + index + " : ");
}
}
if (input.equals("2")) { // MIDDLE DATA ADDITION MENU
index = 1;
System.out.println("#### \tMIDDLE DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("\tData " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enter the input to the middle of the list
numbers = scan.nextInt();
dllist.addMiddle(numbers);
index++;
System.out.print("\tData " + index + " : ");
}
}
if (input.equals("3")) { // LAST DATA ADDITION MENU
index = 1;
System.out.println("#### \tLAST DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("\tData " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enter the input to the last node of the list
numbers = scan.nextInt();
dllist.addLast(numbers);
index++;
System.out.print("\tData " + index + " : ");
}
}
if (input.equals("4")) { // PRINT DATA
dllist.printList(asc);
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("5")) { // RETURN
break;
}
}
}
if (input.equals("2")) { // DATA REMOVAL MENU
while (true) {
System.out.println("#### \tDATA REMOVAL OPERATIONS\t####");
System.out.println("\t1. Remove First Data" + "\n\t2. Remove Middle Data"
+ "\n\t3. Remove Last Data" + "\n\t4. Print Data" + "\n\t5. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // FIRST DATA REMOVAL MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to delete data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tAre you sure you want to delete the first data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Removes the first data
dllist.removeFirst();
System.out.println("\tData has been deleted\n");
}
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("2")) { // MIDDLE DATA REMOVAL MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to delete data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tAre you sure you want to delete the middle data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Removes the middle data
dllist.removeMid();
System.out.println("\tData has been deleted\n");
}
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("3")) { // LAST DATA REMOVAL MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to delete data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tAre you sure you want to delete the last data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Removes the last data
dllist.removeLast();
System.out.println("\tData has been deleted\n");
}
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("4")) { // PRINT DATA
dllist.printList(asc);
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("5")) { // RETURN
break;
}
}
}
if (input.equals("3")) { // DATA REPLACEMENT/FINDER MENU
while (true) {
System.out.println("#### \tDATA REPLACEMENT/FINDER OPERATIONS\t####");
System.out.println(
"\t1. Replace Data" + "\n\t2. Find Data" + "\n\t3. Print Data" + "\n\t4. RETURN");
System.out.print("\tPlease choose [1/2/3/4]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // REPLACE DATA MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to replace data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tDo you want to replace data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Starts replacement process
System.out.print("\tEnter the Numbers you want to Replace : ");
int replace = scan.nextInt();
if (replace < 0 || replace > Integer.MAX_VALUE) { // Checks the input value
System.out
.println("\tThe value of " + replace + "is incorrect, try again\n");
} else {
System.out.print("\tEnter Data : ");
int number = scan.nextInt();
// Replaces the number in the list with the input
dllist.replace(replace, number);
System.out.println("\tData has been replaced\n");
}
}
if (input.equalsIgnoreCase("q")) { // Breaks the replacement loop
System.out.println();
break;
}
}
}
if (input.equals("2")) { // FIND DATA MENU
while (true) {
System.out.print("\tEnter the number you want to find : ");
int number = scan.nextInt();
if (number < 0 || number > Integer.MAX_VALUE) { // Checks the input value
System.out.println("\n\tThe value of " + number + " is incorrect, try again\n");
} else { // Finds the data in the list with the input
System.out.println("\tIs the value " + number + " in the list?: "
+ dllist.find(number) + "\n");
}
System.out.println("\t- Input Q to exit");
System.out.print("\t Do you want to find data again? Input anything to continue: ");
input = scan.next();
if (input.equalsIgnoreCase("q")) { // Breaks the find data loop
System.out.println();
break;
}
}
}
if (input.equals("3")) { // PRINT DATA
dllist.printList(asc);
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("4")) { // RETURN
break;
}
}
}
if (input.equals("4")) { // STATISTICS MENU
while (true) {
System.out.println("#### \tSTATISTICS OPERATIONS\t####");
System.out.println("\t1. Sum Data" + "\n\t2. Average Data" + "\n\t3. Median" + "\n\t4. Max"
+ "\n\t5. Min" + "\n\t6. Variance" + "\n\t7. Standard Deviation"
+ "\n\t8. Print Data" + "\n\t9. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5/6/7/8/9]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // SUM MENU
System.out.println("\tThe sum of the list is " + dllist.sum());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("2")) { // AVERAGE MENU
System.out.println("\tThe average of the list is " + dllist.average());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("3")) { // MEDIAN MENU
System.out.println("\tThe median of the list is " + dllist.median());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("4")) { // MAX MENU
System.out.println("\tThe Max of the list is " + dllist.max());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("5")) { // MIN MENU
System.out.println("\tThe Min of the list is " + dllist.min());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("6")) { // VARIANCE MENU
System.out.println("\tThe Variance of the list is " + dllist.variance());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("7")) { // STANDARD DEVIATION MENU
System.out.println(
"\tThe Standard Deviation of the list is " + dllist.standardDeviation());
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("8")) { // PRINT DATA
dllist.printList(asc);
System.out.print("\n\tInput anything to RETURN: ");
input = scan.next();
}
if (input.equals("9")) { // RETURN
break;
}
}
}
if (input.equals("5")) // RETURN
break;
}
}
if (input.equals("2")) { // PACKAGE MENU
@SuppressWarnings({ "rawtypes" })
LinkedList llist = new LinkedList();
while (true) {
System.out.println("#### \tPACKAGED OPERATIONS\t####");
System.out.println(
"\t1. Add Data" + "\n\t2. Remove Data" + "\n\t3. Replace/Find Data" + "\n\t4. RETURN");
System.out.print("\tPlease choose [1/2/3/4]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // DATA ADDITION MENU
while (true) {
System.out.println("#### \tDATA ADDITION OPERATIONS\t####");
System.out.println("\t1. Add First Data" + "\n\t2. Add Middle Data" + "\n\t3. Add Last Data"
+ "\n\t4. Print Data" + "\n\t5. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // FIRST DATA ADDITION MENU
index = 1;
System.out.println("#### \tFIRST DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("Data " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enter the input to the first node of the list
numbers = scan.nextInt();
llist.addFirst(numbers);
index++;
System.out.print("Data " + index + " : ");
}
}
if (input.equals("2")) { // MIDDLE DATA ADDITION MENU
index = 1;
System.out.println("#### \tMIDDLE DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("Data " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enter the input to the middle of the list
int number = scan.nextInt();
llist.add(number);
index++;
System.out.print("Data " + index + " : ");
}
}
if (input.equals("3")) { // LAST DATA ADDITION MENU
index = 1;
System.out.println("#### \tLAST DATA ADDITION OPERATIONS\t####");
System.out.println("INFO:\t- Enter a number data (Integer)"
+ "\n\t- Input a non-integer to RETURN\n");
System.out.print("Data " + index + " : ");
// Loops until the input is not an integer
while (scan.hasNextInt()) {
// Enter the input to the last node of the list
int number = scan.nextInt();
llist.addLast(number);
index++;
System.out.print("Data " + index + " : ");
}
}
if (input.equals("4")) { // PRINT DATA
while (true) {
System.out.println("\tData : " + llist + "\n");
System.out.print("INFO:\t- Input Q to RETURN : ");
String Return = scan.next();
System.out.println();
if (Return.equalsIgnoreCase("q")) {
break;
}
}
}
if (input.equals("5")) { // RETURN
break;
}
}
}
if (input.equals("2")) { // DATA REMOVAL MENU
while (true) {
System.out.println("#### \tDATA REMOVAL OPERATIONS\t####");
System.out.println("\t1. Remove First Data" + "\n\t2. Remove Middle Data"
+ "\n\t3. Remove Last Data" + "\n\t4. Print Data" + "\n\t5. RETURN");
System.out.print("\tPlease choose [1/2/3/4/5]: ");
input = scan.next();
System.out.println();
if (input.equals("1")) { // FIRST DATA REMOVAL MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to delete data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tAre you sure you want to delete the first data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Deletes the first data
llist.removeFirst();
System.out.println("\tData has been deleted\n");
}
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("2")) { // MIDDLE DATA REMOVAL MENU
while (true) {
System.out.print("\tEnter index of the number you want to delete : ");
int indexMid = scan.nextInt();
System.out.println();
if (indexMid >= 0 && indexMid <= llist.size() - 1) { // Checks if the index is in
// the list
llist.remove(indexMid);
System.out.println("\tData has been deleted");
} else
System.out.println("\t Data index incorrect ");
System.out.println("INFO:\t- Input Q to RETURN\n");
System.out.print("\tDo you still want to delete any data (press any key)? : ");
input = scan.next();
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("3")) { // LAST DATA REMOVAL MENU
while (true) {
System.out.println(
"INFO:\t- Enter Y to delete data (Integer)" + "\n\t- Input Q to RETURN\n");
System.out.print("\tAre you sure you want to delete the last data? : ");
input = scan.next();
if (input.equalsIgnoreCase("y")) { // Deletes the last data
llist.removeLast();
}
if (input.equalsIgnoreCase("q")) { // Breaks the removal loop
break;
}
}
}
if (input.equals("4")) { // PRINT DATA
while (true) {
System.out.println("\tData : " + llist + "\n");
System.out.print("INFO:\t- Input Q to RETURN : ");
String back = scan.next();
System.out.println();
if (back.equalsIgnoreCase("q")) {
break;
}
}
}
if (input.equals("5")) { // RETURN
break;
}
}
}
if (input.equals("3")) { // DATA REPLACEMENT/FINDER MENU
while (true) {
System.out.println("#### \tDATA REPLACEMENT/FINDER OPERATIONS\t####");
System.out.println(
"\t1. Replace Data" + "\n\t2. Find Data" + "\n\t3. Print Data" + "\n\t4. RETURN");
System.out.print("\tPlease choose [1/2/3/4]: ");
input = scan.next();
System.out.println();