Question

JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include...

JAVA

Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include the following method: public ExtLinkedList firstHalfList() which returns the first half of the list. In other words, if you have a ExtLinkedList of size 5, for example, the method firstHalfList should return the list with the first two nodes. If it is a list of size 6, it should return the list with the first three nodes. The original list should not be modified. Also, note that the numbers 5 and 6 are only provided for illustration purposes – your code should work for any size including the empty list and any parameter E. Also, the original LinkedList should remain intact and not have half its contents deleted. Estimate the run-time complexity of the method given the size of the original list is n.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package learning;

import java.util.LinkedList;

/***
*Making a new class ExtLinkedList which will extend LinkedList and
*making it generic so that it can work for any type of list
*/


public class ExtLinkedList<E> extends LinkedList<E>{

/***
*
* @return new List which will be half of the original list
*/

public ExtLinkedList<E> firstHalfList(){
  // Create the list which will contain the half of the elements of original list
  ExtLinkedList<E> firstHalf = new ExtLinkedList<E>();
  // First size needs to be find of the original list
  int size = this.size();
  for(int i =0; i < (size)/2 ; i++){ //
   firstHalf.add(this.get(i));
  }
  return firstHalf;
}


public static void main(String args[]){
  ExtLinkedList<String> myList = new ExtLinkedList<String>();
  myList.add("Nitin");
  myList.add("Maryada");
  myList.add("Bunty");
  myList.add("Sachin");
  myList.add("AAni");
  myList.add("Saumil");
  System.out.println("New Half List is: " + myList.firstHalfList());
  System.out.println("Original List: " + myList);
}

}

/**** Run time Compexity will be o(n) as we will be traversing n/2 elements ***/

Add a comment
Know the answer?
Add Answer to:
JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please make sure the code works perfectly and is accurate. This is high-stake assignment. I will...

    Please make sure the code works perfectly and is accurate. This is high-stake assignment. I will rate highly, thank you very much for saving my life. Question: Create a Java program that extends the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would have these methods: public class ExtLinkedList<E> extends LinkedList<E> { public ExtLinkedList<E>subList() { .... (stuff goes here) } } which returns the values stored at index 0,3,6,9, … of the list. If given an empty list it should simply...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute....

    Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute. The constructor will simply create an empty list. This will be a different LinkedList, as all of the items will be in ascending numerical order. That means items will not necessarily be placed at the end of the list, but placed where it should be located. Note that the iterator simply uses the next() method, so if you use the iterator to find the...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList()...

    P1 is below package p6_linkedList; import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null) { header...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT