Question

In: Computer Science

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 list.mergeThreeLists(list1,list2) will have (5,3,1,8,10,12,14,22,23,24, 25,26). Obviously the size of the returned list will be equal to list.size()+list1.size()+list2.size(). Use listIterator() to do this efficiently for full credit.  Your code should work for any size including the empty list and any parameter E.

is there an applicable code? if so, what is it?

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

import java.util.Iterator;
import java.util.LinkedList;

class ExtLinkedList<E> extends LinkedList<E> {
        public ExtLinkedList<E> mergeThreeLists(ExtLinkedList<E> list1, ExtLinkedList<E> list2) {
                // empty list
                ExtLinkedList<E> ll = new ExtLinkedList<>();

                // calculating the size of the merged list
                int size = this.size() + list1.size() + list2.size();

                // creating iterators
                Iterator<E> i1 = this.iterator();
                Iterator<E> i2 = list1.iterator();
                Iterator<E> i3 = list2.iterator();

                // main loop
                for (int i = 0; i < size; i++) {
                        // adding data from the list
                        if (i1.hasNext())
                                ll.add(i1.next());
                        else if (i2.hasNext())
                                ll.add(i2.next());
                        else if (i3.hasNext())
                                ll.add(i3.next());
                }

                return ll;
        }
}

public class Test {
        public static void main(String[] args) {
                // testing the code
                ExtLinkedList<Integer> list = new ExtLinkedList<>();
                list.add(5);
                list.add(3);
                list.add(1);

                ExtLinkedList<Integer> list1 = new ExtLinkedList<>();
                list1.add(8);
                list1.add(10);
                list1.add(12);
                list1.add(14);

                ExtLinkedList<Integer> list2 = new ExtLinkedList<>();
                list2.add(22);
                list2.add(23);
                list2.add(24);
                list2.add(25);
                list2.add(26);

                System.out.println(list.mergeThreeLists(list1, list2));

        }
}

Output


Related Solutions

1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
1.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 this list, followed by values from list1, and then followed by values from list2. For example, if E is Integer type and this list has (5,3,1), list1 has (8, 10,12,14), and list2 has (22,23,24,25,26) in that order, then the returned...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
write a program in java that contain a class for botique . data member include code...
write a program in java that contain a class for botique . data member include code , color , size , quantity . your class should contains all accessor and mutator methods , non paraqmetric constructor , parametric constructor , input andvidsplay method
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
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...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList are two different classes that are used to create the assignment.
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
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT