Question

In: Computer Science

Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...

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.

Solutions

Expert Solution

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
=============================


Related Solutions

In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides for lecture 8). Add the following methods to the class and submit the completed LinkedList class. int size() which returns the size of the linked list Link getElementByIndex(int index) which returns the Link/node specified by the index. For example, getElementByIndex(0) should return the first Link, getElementByIndex(2) should return the third Link. If index is not in range, your method should return null. boolean hasDuplicate()...
Create a generic method to print objects in java. Include a tester class.
Create a generic method to print objects in java. Include a tester class.
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT