6 Complete the methods below 7None of the methods should modify the list, unless
ID: 3909228 • Letter: 6
Question
6 Complete the methods below 7None of the methods should modify the list, unless that is the purpose of the method * You may not add any fields to the node or list classes. * You may not add any methods to the node class. 9 12 You MAY add private methods to the list class (helper functions for the recursion) 13 *1 14 ? public class MyLinked { 15static class Node 16 17 18 19 20 21 public Node (double item, Node next) public double item; public Node next; this.item -item; this.next - next; h int N; Node first;Explanation / Answer
Given below is the code for the 2 methods. Please do rate the answer if it was helpful. Thank you
public int positionOfFirstFiveFromBeginning(){
Node current = first;
int pos = 0;
while(current != null){
if(current.item == 5.0) //found 5.0 in list
return pos;
pos++;
current = current.next;
}
return -1; //when not found
}
public int positionOfLastFiveFromEnd(){
int pos = 0;
int indexFromBeginning = -1;
while(current != null){
if(current.item == 5.0) //found 5.0 in list, save the index
indexFromBeginning = pos;
pos++;
current = current.next;
}
if(indexFromBeginning == -1) //did not find any 5.0 in list
return -1;
else
return N-i-1; //using the total no. of elements in list and index from beginning, find the index from end
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.