In: Computer Science
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;
// remove all occurrences of item from the
list
public void remove (double item) {
// TODO
}
Write the remove function. Do NOT add any fields to the node/list classes, do not add methods to the Node class,
and only one loop/recursion allowed if needed. Cannot use contains() or other functions.
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;
// remove all occurrences of item from the
list
public void remove(double item) {
// if list is
empty
if(N == 0) {
return; }
// First we will
check all nodes starting from second node
Node prev =
first;
Node current =
first.next;
while(current !=
null) {
if(current.item
== item) {
prev.next
= current.next;
N--;
}
else {
prev
= current;
}
current
= current.next;
}
// check head now,
and change first variable if needed
if(first.item ==
item) {
N--;
first
= first.next;
}
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.