Question: //CODE public class RemoveAfterProblem { private static int countClears = 0; private static int countSetNext = 0; public static class Node { private E data;

//CODE public class RemoveAfterProblem { private static int countClears = 0; private//CODE

public class RemoveAfterProblem {

private static int countClears = 0;

private static int countSetNext = 0;

public static class Node {

private E data;

private Node next;

public Node(E data, Node next) {

this.data = data;

this.next = next;

}

public Node(E data) {

this(data, null);

}

public E getData() {

return data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

countSetNext++; // for grading purposes

this.next = next;

}

public void clear() {

countClears++; // for grading purposes

data = null;

next = null;

}

}

public int getCountClears() {

return countClears;

}

public int getCountSetNext() {

return countSetNext;

}

public void setCountClears(int count) {

countClears = count;

}

public void setCountSetNext(int count) {

countSetNext = count;

}

/**

* TODO EXERCISE 4:

*

* Implement a method that removes from the list all elements, if any,

* that are immediately after one occurrence of a given

* element and which are not equal to the given element.

*

* The method receives as a parameter a Node that represents the head of a linked list,

* as well as a target which will be used to find nodes containing that element such that

* we can remove the subsequent nodes that one (if they subsequent node does not contain an

* element that is equal to target).

*

* For example, assume that the specified element is 5.

*

* 1. If the list is (4, 5, 5, 6, 8, 5, 9, 5), then

* the list remaining would be (4, 5, 5, 8, 5, 5).

* The method would return value 2 (number of removals).

*

* 2. If the list is (4, 3, 6, 7, 8), the the list is

* not altered because it has no occurrence of 5. And

* the method would return 0.

*

* Your solution must be as efficient as possible and must

* properly apply the clear() method (of class Node) to each

* deleted node.

*

* @param

* @param first - Head Node of the Linked List

* @param target - Target element in a node to find that will

* remove nodes after it that are not equal to target

* @return The number of copies the method removed

*/

public int removeAfter(Node first, E target ) {

/*TODO ADD YOUR CODE HERE*/

return -Integer.MAX_VALUE; //Dummy Return

}

}

4. (20 pts) Head over to RemoveAfterproblem. java after finishing exercise 3. The instructions for this exercise are as follows: - Implement a method called removeafter () that removes from the List all elements, if any, that are immediately after one occurrence of a given element and which are not equal to the given element. - The method receives as a parameter a Node that represents the head of a linked list, as well as a target which represents the element we are searching for such that we can remove the subsequent node immediately after that one. - Note: If the subsequent node contains the target element it will not be removed, we only remove nodes whose value is different from target. - Example, assume that a call to removeafter (head, 5) is made. 1. If head is referencing the List {4,5,5,6,8,5,9,5}, then the List remaining would be {4,5,5,8,5,5}. and the method would return value 2 (number of removals). 2. If head is referencing the List {4,3,6,7,8}, then the List is not altered because it has no occurrence of 5 , and the method would return 0. - Your solution must be as efficient as possible and must properly apply the clear() method (of class Node) to each deleted node

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!