In: Computer Science
Use LinkedList build-in class (java.util.LinkedList) to write a
Java program that has:
A. Method to print all elements of a linked list in order. B.
Method to print all elements of a linked list in reverse order. C.
Method to print all elements of a linked list in order starting
from specific position. D. Method to join two linked lists into the
first list in the parameters. E. Method to clone a linked list. The
copy list has to be returned as an output of the method. F. Method
to search and return position of an element in the list. Return
-999 if it is not exist. G. Method to compare between two linked
lists. Return true if they are equal. H. Method to convert a linked
list into an array. The method should return the array as an
output. I. test method to : 1. Append {12, 44, 11, 2, 201, 2200,
332, 5, 112} to the end of list1. 2. Print all elements in list1.
3. Print all elements in list1starting at position 4. 4. Insert the
specified element at position 2 in list1. 5. Print all elements in
reverse order for list1. 6. Insert 99 into list1 at the first
position and 777 at the last position. 7. Print the second largest
element in list1 and print its position. 8. Remove element 201 from
list1. 9. Remove first and last element from list1. 10. Print all
elements in list1. 11. Swap positions between 11 and 2200 in the
linked list 12. Append {45, 56, 67, 78, 89} to the end of a linked
list list2. 13. Clone list1 to list3. 14. Compare between list1 and
list2. 15. Compare between list1 and list3. 16. Join list1 and
list2 to list1 17. Print all elements in list1. 18. Check if 99,
777 and 11 are exists in list1. Each as separate case. 19. Convert
list1 into array1. Then print array1 20. Remove all the elements
from list3.
SOURCE CODE: *Please follow the comments to better understand the code. **Please look at the Screenshot below and use this code to copy-paste. ***The code in the below screenshot is neatly indented for better understanding. import java.util.Arrays; import java.util.LinkedList; public class TestLL { public static void main(String[] args) { // 1 LinkedList<Integer> list1 = new LinkedList<Integer>(Arrays.asList(12, 44, 11, 2, 201, 2200, 332, 5, 112)); // 2 printElements(list1); // 3 printElements(list1,4); // 4 insert(list1,2,100); // 5 printElementsReverse(list1); // 6 insert(list1,1,99); insertLast(list1,777); // 7 printSecondLargest(list1); // 8 remove(list1,201); // 9 removeFirst(list1); removeLast(list1); // 10 printElements(list1); // 11 swapPositions(list1,11,2200); // 12 LinkedList<Integer> list2 = new LinkedList<>(Arrays.asList(45, 56, 67, 78, 89)); // 13 LinkedList<Integer> list3 = clone(list1); // 14 System.out.println("List1 and 2 Equal?? "+compare(list1,list2)); // 15 System.out.println("List1 and 3 Equal?? "+compare(list1,list3)); // 16 join(list1, list1, list2); // 17 printElements(list1); // 18 System.out.println("99 exists in list1? "+exists(list1,99)); System.out.println("777 exists in list1? "+exists(list1,777)); System.out.println("11 exists in list1? "+exists(list1,11)); // 19 Integer[] array1=convertToArray(list1); // 20 printArray(array1); // 21 removeAll(list3); } private static void removeAll(LinkedList<Integer> list3) { list3.removeAll(list3); } private static void printArray(Integer[] array1) { for(int i=0;i<array1.length;i++) System.out.print(array1[i]+" "); System.out.println(); } private static Integer[] convertToArray(LinkedList<Integer> list1) { return list1.toArray(new Integer[list1.size()]); } private static boolean exists(LinkedList<Integer> list1, int element) { return list1.contains(element); } private static void join(LinkedList<Integer> list1, LinkedList<Integer> list11, LinkedList<Integer> list2) { list1.addAll(list1); list1.addAll(list2); } private static boolean compare(LinkedList<Integer> list1, LinkedList<Integer> list2) { if(list1.size()!=list2.size()) return false; for(int i=0;i<list1.size();i++) if(list1.get(i)!=list1.get(i)) return false; return true; } private static LinkedList<Integer> clone(LinkedList<Integer> list1) { return (LinkedList<Integer>) list1.clone(); } private static void swapPositions(LinkedList<Integer> list1, int a, int b) { int i1=list1.indexOf(a); int i2=list1.indexOf(b); list1.set(i2,a); list1.set(i1,b); } private static void removeFirst(LinkedList<Integer> list1) { list1.removeFirst(); } private static void removeLast(LinkedList<Integer> list1) { list1.removeLast(); } private static void remove(LinkedList<Integer> list1, int element) { list1.remove(Integer.valueOf(201)); } private static void printSecondLargest(LinkedList<Integer> list1) { int max=list1.get(0); int maxIndex=0; for(int i=1;i<list1.size();i++) if(list1.get(i)>max) { max=list1.get(i); maxIndex=i; } System.out.println("Maximum number: "+max+" Index= "+maxIndex); } private static void insertLast(LinkedList<Integer> list1, int element) { list1.add(element); } private static void printElementsReverse(LinkedList<Integer> list1) { for(int i=list1.size()-1;i>=0;i--) System.out.print(list1.get(i)+" "); System.out.println(); } private static void insert(LinkedList<Integer> list1, int index, int element) { list1.add(index,element); } private static void printElements(LinkedList<Integer> list1, int start) { for(int i=start;i<list1.size();i++) System.out.print(list1.get(i)+" "); System.out.println(); } private static void printElements(LinkedList<Integer> list1) { for(int i:list1) System.out.print(i+" "); System.out.println(); } }
===============================
OUTPUT:
12 44 11 2 201 2200 332 5 112
201 2200 332 5 112
112 5 332 2200 201 2 11 100 44 12
Maximum number: 2200 Index= 7
99 44 100 11 2 2200 332 5 112
List1 and 2 Equal?? false
List1 and 3 Equal?? true
99 44 100 2200 2 11 332 5 112 99 44 100 2200 2 11 332 5 112 45 56
67 78 89
99 exists in list1? true
777 exists in list1? false
11 exists in list1? true
99 44 100 2200 2 11 332 5 112 99 44 100 2200 2 11 332 5 112 45 56
67 78 89
Process finished with exit code 0
=============================