Question: set(int index , E elem ) This will implement List's set method. It will need to replace the element at the given index and return

set(int index, E elem )

This will implement List's set method. It will need to replace the element at the given index and return the previous element at that index OR, if index is not a valid index for this list, throw an IndexOutOfBoundsException. The algorithm you must implement is:

 If index is not a valid index Raise an IndexOutOfBoundsException End if Set trav to the first Entry in the linked list Set count equal to 0 Traverse the list until you find the Entry at index index // This is the usual list traversal algorithm Set retVal equal to trav's element Set trav's element equal to elem Return retVal 

contains(Object obj)

This method returns true if the list has obj as an element and false otherwise. The algorithm you must implement is:

 Set trav to the first Entry in the linked list For index = 0 to size If (trav's element and obj are equal) return true End if Set trav equal to the next Entry in the list End for return false 

You SHOULD either use the class of test cases, DoublyLinkedListTests, in Activity06.jar to check your code. Remember that Web-CAT uses the last submission as your grade. Be certain to submit your code once you finish part 1 of this assignment. Each method in part 1 is worth 40% of the activity grade, so this is worth a total of 80% of the overall score.

Part 2: This is worth the final 20% of your overall score. Complete the addAll() method. I already added code to loop through each element in coll. Inside this loop add code so that each element is added to the end of the list (e.g., including the other elements it has already added).

CODE:

package edu.buffalo.cse116; import java.util.AbstractList; import java.util.Arrays; import java.util.Collection; import java.util.List; /** * This defines a few basic methods in a doubly linked-based List. This is now much closer to being a working List * implementation. Students will be writing the code to demonstrate they understand how to add nodes to a doubly linked * list. * * @author Matthew Hertz * @param Type of data held in this collection */ public class DoublyLinkedList extends AbstractList { /** * Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Reference to the last node in our linked list. This will be null if the list is empty. */ private Entry tail; /** * This size instance variable specifies the number of elements in the List. We could instead recompute this as * needed, but adding it costs a little space and makes our code much more efficient. This space v. time tradeoff is a * common issue in computer science. */ private int size; /** * Creates an empty list. */ public DoublyLinkedList() { reset(); }

/** * This method, which is only used within the DoublyLinkedList class, returns the instance to its initial state. This * call will reset both head and tail to be null and sets the size to be 0. */ private void reset() { head = null; tail = null; size = 0; } /** * This returns if the specified object is an element within the linked list. It will return true if it is one of the * elements and false otherwise. * * @param obj Potentially null object for which we are searching * @return True when obj is an element in this List; false otherwise. */ @Override public boolean contains(Object obj) { } /** * Reassigns the element at the specified index in this list to the new value. * * @param index List location whose element should be returned. * @param elem Element that will now be assigned to this index in the list * @return Element that was previously at the specified index in this list */ @Override public E set(int index, E elem) { } /** * Adds all of the elements in {@code coll} to the end of the linked list. The elements must be added in the order the * appear in a for-each loop over {@code coll}. If {@code coll} is the current linked list, then this method's * behavior is not defined (this is a fancy way of saying whatever happens as a result of a call like * llist.addAll(llist) is NOT OUR FAULT(TM). * * @param coll Collection whose elements will be added to the end of the linked list. * @return True if at least one element is added; false otherwise. */ @Override public boolean addAll(Collection coll) { for (E elem : coll) { // TODO: Add your code here } return true; } /** * Returns the number of elements currently in this list. * * @return the number of elements in the list */ @Override public int size() { return size; } @Override public E get(int index) { throw new UnsupportedOperationException(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!