6 Complete the methods below 7None of the methods should modify the list, unless
ID: 3909260 • 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
public class MyLinked {
static class Node {
public Node (double item, Node next) { this.item = item; this.next = next; }
public double item;
public Node next;
}
int N;
Node first;
public int positionOfFirstFiveFromBeginning () {
Node curr = first;
int index = -1 , i = 0;
while(curr != null) {
if(curr.item==5) {
return i;
}
++i ;
curr = curr.next;
}
return index;
}
public int positionOfLastFiveFromEnd () {
Node curr = first;
int index = -1 , i = 0;
while(curr != null) {
if(curr.item==5) {
index = i;
}
++i ;
curr = curr.next;
}
if(index != -1)
return i-index-1;
return index;
}
============================
Let me know if it passes all the test. Comment and I will respond to help if it doesn't.
I will help, PLEASE UPVOTE if helpful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.