Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

set(int index , E elem ) This will implement List\'s set method. It will need to

ID: 3808364 • Letter: S

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 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:

contains(Object obj)

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

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();
}

}

Explanation / Answer

HI, I have answered Q1.

I have implemeted both methods.

Please repost others in separate post.

/**

* 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) {

      

       Entry trav = head;

       int index = 0;

       while(index < size){      

           if(trav.getElement().compareTo(obj) == 0)

               return true;

           trav = trav.getNext();

           index++;

       }

      

       return false;

   }

   /**

   * 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) {

      

       if(index >= size){

           throw new IndexOutOfBoundsException();

       }

      

       int count = 0;

       Entry trav = head;

      

       while(count < index)

           trav = trav.getNext();

      

       E retVal = trav.getElement();

       trav.setElement(elem);

      

       return retVal;

      

   }

   /**

   * 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) {

           Entry<E> newE= new Entry<>(elem);

           tail.setNext(newE);

           tail = tail.getNext();

       }

       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();

   }

}