Question

Create an ExtLinkedList class by extending the LinkedList class to include the following methods and estimate...

Create an ExtLinkedList class by extending the LinkedList class to include the following methods and estimate the run-time complexity of each method.

3. public ExtLinkedList evenList()

// gets the elements E in locations 0,2,4,6,… of this list and puts them in a new ExtLinkedList in that order and returns it

// make sure to handle errors

4. public ExtLinkedList intsersect (ExtLinkedList eli)

// picks out elements that are common to this ExtLinkedList and eli

// and returns them in the form of a new ExtLinkedList

// once again, you can use equals () method // the new list should not have duplicate elements

// handle errors or exceptional situations appropriately.

// example: this: {1,3,4,4}, parameter : {1,4,5,7} then return {1,4}

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ExtLinkedList.java

import java.util.Iterator;

import java.util.LinkedList;

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

      // returns the elements in even indices as a new list

      // runtime complexity is O(n) since this will loop through the list once

      public ExtLinkedList<T> evenList() {

            // creating a new list

            ExtLinkedList<T> result = new ExtLinkedList<T>();

            // getting an iterator for this list

            Iterator<T> it = iterator();

            int i = 0;

            // looping through each object

            while (it.hasNext()) {

                  // getting element

                  T element = it.next();

                  // if i is even, adding to result

                  if (i % 2 == 0) {

                        // this will take O(1) time

                        result.addLast(element);

                  }

                  i++;

            }

            return result;

      }

      // returns the intersection of this list and another

      // The runtime complexity is O(n^2) or O(2n^2) (I'm not really sure since in

      // every iteration of the loop, it will use O(n) time for checking if

      // element exist on other list and O(n) time for checking if the element

      // exist on result list (to avoid duplication))

      public ExtLinkedList<T> intsersect(ExtLinkedList<T> eli) {

            if (eli == null) {

                  // invalid parameter

                  return null;

            }

            ExtLinkedList<T> result = new ExtLinkedList<T>();

            // getting an iterator and looping through the list

            Iterator<T> it = iterator();

            while (it.hasNext()) {

                  T element = it.next();

                  // if this element exist on other list and not added before, adding

                  // it to result

                  if (eli.contains(element) && !result.contains(element)) {

                        result.addLast(element);

                  }

            }

            return result;

      }

}

Know the answer?
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
Similar Questions
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with...
1. Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. 2. Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. 3. You have been given the job of creating a new order processing system for the Yummy Fruit CompanyTM. The system reads pricing information for the various delicious...
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT