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

This is a incomplete implementation of class TheList in Java. TheList object is

ID: 3710728 • Letter: T

Question

This is a incomplete implementation of class TheList in Java. TheList object is a singly-linked list. Where the first node is referenced by the var head, and the last node is referenced by the var tail.

class ListNode<E>{

public ListNode(E data, ListNode<E> next){

this.data = data;

this.next = next;

}

public E data;

public ListNode<E> next;

}

class TheList<E> implements List<E>{

...

private ListNode<E> head, tail;

}

Implement this function in Java in the class TheList:

IndexOf(element): this function returns the index of the first occurrence of element in the list. it returns -1 if element does not occur in the list.

Explanation / Answer

IndexOf(element) method

@Override

public int indexOf(Object elem){

//Initialize temp with head

ListNode<E> temp = head;

int i=0;

//Loop till end of the list

while(temp!=tail){

if(temp.data.equals(elem)){

return i;

}

temp = temp.next;

i++;

}

//compare with tail

if(tail.data.equals(elem)){

return i;

}

return -1;

}

** Couldn't test the method because other methods implementation is not provided (eg: add)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote