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

public boolean contains(Object o) { // Implement it in this exercise Node curren

ID: 3809508 • Letter: P

Question

public boolean contains(Object o) {

// Implement it in this exercise

Node current = head;

for (int i = 0; i < size; i++)

{

if (current.element.equals(o))

return true;

current = current.next;

}

return false;

}

/** Returns the index of the first matching element in this list.

* Returns -1 if no match. */

public int indexOf(Object o)

{

// Implement it in this exercise

Node current = head;

for (int i = 0; i < size; i++) {

if (current.element.equals(o))

return i; current = current.next;

}

return -1;

}

/** Return the element from this list at the specified index */

public E get(int index)

{

// Your code here!

}

/** Returns the index of the last matching element in this list

* Returns -1 if no match. */

public int lastIndexOf(Object o)

{

// Your code here!

}

/** Return and Replace the element at the specified position in this list

* with the specified element. */

public E set(int index, E e)

{

// Your code here!

}

head, tail get First remove First LISTING 24.6 My LinkedList.java 1 public class My LinkedList extends MyAbstractList private NodexEx head, tail Create a default list public MyLinkedListo Create a list from an array of objects 9 public MyLinkedList(ECJ objects) super objects): 13 Return the head element in the list 14 public E getFirsto 15 if C 0) return null: 17 else 19 return head element 20 21 22 23 Return the last element in the list public E getLasto 2S if C 0) 26 return null 27 28 else 29 return tail-element 31 33 Add an element to the beginning of the list 34 public void addFirstCE e) 3S Implemented in Section 24.4-3-1, so omitted here 37 38 Add an element to the end of the list 39 public void addLastCE e) Implemented in Section 24.4-3-2, so omitted here 41 43 Goverride Add a new element at the specified index in this list The index of the head element is 0 4S public void addCint index E e) Implemented in Section 24-4-3-3, so omitted here 47 49 Remove the head node and 50 return the object that is contained in the removed node. si public E remove First O 52 Implemented in Section 24.4.3.4, so omitted here S3

Explanation / Answer

public boolean contains(Object o) {
   // Implement it in this exercise
   Node current = head;
   for (int i = 0; i < size; i++)
   {
       if (current.element.equals(o))
           return true;
       current = current.next;
   }
   return false;
}
/** Returns the index of the first matching element in this list.
* Returns -1 if no match. */
public int indexOf(Object o)
{
   // Implement it in this exercise
   Node current = head;
   for (int i = 0; i < size; i++) {
       if (current.element.equals(o))
           return i;
       current = current.next;
   }
   return -1;
}

/** Return the element from this list at the specified index
* null if index doesn't match any element*/
public E get(int index)
{
   // Implement it in this exercise
   Node current = head;
   for (int i = 0; i < size; i++) {
       if (i == index)
           return current.element;
       current = current.next;
   }
   return null;
}

/** Returns the index of the last matching element in this list
* Returns -1 if no match. */
public int lastIndexOf(Object o)
{
   index = -1;
   // Implement it in this exercise
   Node current = head;
   for (int i = 0; i < size; i++) {
       // overwrite the index by elements which come later in the list
       if (current.element.equals(o))
           index = i;
      
       current = current.next;
   }
   return index;  
}

/** Return and Replace the element at the specified position in this list
* with the specified element. Returns null if invalid index*/
public E set(int index, E e)
{
   E elementToReturn = null;
   Node current = head;
  
   for (int i = 0; i < size; i++) {
       if (i == index) {
           elementToReturn = current.element;
           current.element = e;
           return elementToReturn;
       }      
       current = current.next;
   }
  
   return elementToReturn;
}