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....
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 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
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT