6 Complete the methods below 7None of the methods should modify the list, unless
ID: 3909229 • 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
Following is the answer:
public void deleteFirst(){
if(first == null){
return first;
}
Node temp = first;
Node next = temp.next.next;
temp.next = next; // Unlink the deleted node from list
}
public void delete(int k) {
if(k<0 || k >= N) throw new IllegalArgumentException ();
if (first == null)
return;
// Store first node
Node temp = head;
// If first needs to be removed
if (k == 0)
{
head = temp.next; // Change first
return;
}
// Find previous node of the node to be deleted
for (int i=0; temp!=null && i<k-1; i++)
temp = temp.next;
// If k is more than number of nodes
if (temp == null || temp.next == null)
return;
Node next = temp.next.next;
temp.next = next; // Unlink the deleted node from list
}
public void reverse() {
Node prev = null;
Node current = first;
Node next = null;
while(current != null){
next = current.next;
current.next = prev;
prev = current;
current = next;
}
first = prev;
}
public void remove(double item){
if(first == null){
System.out.println("Empty");
}
while(first.value == value)
first = first.value;
if(first == null){
System.out.println("Empty");
}
Node prev = first;
Node current = prev.next;
while(current != null){
if (current.value == value)
prev.next = current.next;
prev = prev.next;
current = prev.next;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.