Question

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 return the empty list. For example, subList() method for a non-empty list containing values {e0,e1,e2,e3, e4,e5,e6,e7} should return a list containing (e0, e3,e6}. The original list should not be modified. 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 any of its contents removed. Estimate the run-time complexity of the method given the size of the original list is n.   

No constructor is needed for the class ExtLinkedList<E> class.

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

Program:

import java.util.*;

//class ExtLinkedList
public class ExtLinkedList<E> extends LinkedList<E>
{
   //method to create the sublist
   public ExtLinkedList<E>subList()
   {
       ExtLinkedList<E> sublist = new ExtLinkedList<E>();
      
       ListIterator<E> itr = listIterator();

for(int i = 0; itr.hasNext(); i++)
{
E element = itr.next();
  
if(i % 3 == 0)
{
sublist.add(element);
}
}
return sublist;
   }
}

//class Driver
class Driver
{
   //main method
   public static void main (String[] args)
   {
       //Create the linked list
       ExtLinkedList<String> list = new ExtLinkedList <String>();
  
       //add the elements
       list.addLast("e0");
   list.addLast("e1");
   list.addLast("e2");
   list.addLast("e3");
   list.addLast("e4");
   list.addLast("e5");
   list.addLast("e6");
   list.addLast("e7");
  
   Iterator itr = list.iterator();

       //display the original list
       System.out.print("The original LinkedList: ");
   for(int i = 0; itr.hasNext(); i++)
{
System.out.print(itr.next() + " ");
}

   System.out.println();
ExtLinkedList<String> sublist = list.subList();
  
itr = sublist.iterator();
  
//display the sublist
System.out.print("The sublist: ");
for(int i = 0; itr.hasNext(); i++)
{
System.out.print(itr.next() + " ");
}
   }
}


Output:

The run-time complexity of the method given the size of the original list is n: O(n), since it needs to traverse and access each node of the original list .

N.B. Whether you face problem or you need help then share me in the comment section, I'll be happy to help you.

Add a comment
Know the answer?
Add Answer to:
Please make sure the code works perfectly and is accurate. This is high-stake assignment. I will...
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
  • 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....

  • 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....

  • 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...

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

    PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() {    clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free...

    JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...

  • CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super...

    CAN SOMEONE PLEASE SOLVE THIS? THANK YOU. FINALTREE.JAVA: import java.util.LinkedList; public class FinalTree<E extends Comparable<? super E>> { private Node<E> root; private int size; public FinalTree() { root = null; size = 0; } public boolean isEmpty() { return size == 0; } public void add(E newItem) { if (isEmpty()) { root = new Node<E>(newItem); } else { Node<E> parent = null; Node<E> temp = root; int result = 0; while (temp != null) { parent = temp; result =...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

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