Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello!, im trying to implement a removeAfter method in Java and this is my current implementation I am only failing 1 test case but I

Hello!, im trying to implement a removeAfter method in Java and this is my current implementation I am only failing 1 test case but I don't know where my logic is failing. any help would be great. Implementation: public int removeAfter(Node first, E target ) { /*TODO ADD YOUR CODE HERE*/ //ATTEMPT THAT FAILS REMOVALS2 int counter = 0; Node currentNode = first; Node prevtoCur = null; while (currentNode != null) { if (currentNode.getData().equals(target)) { while (currentNode.getNext() != null && currentNode.getNext().getData().equals(target)) { prevtoCur = currentNode; currentNode = currentNode.getNext(); } prevtoCur.setNext(currentNode.getNext()); currentNode.clear(); currentNode = prevtoCur.getNext(); counter++; } else { prevtoCur = currentNode; currentNode = currentNode.getNext(); } } return counter; }

Instructions:

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.

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).

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

Enhance the international team by recruiting the best people.

Answered: 1 week ago

Question

Know how productivity improvements impact quality and value.

Answered: 1 week ago

Question

Recommend the key methods to improve service productivity.

Answered: 1 week ago