Question: Java Programming: Complete the following method that is passed a String and a chain of nodes containing Strings. The method removes all occurrences of the
Java Programming:
Complete the following method that is passed a String and a chain of nodes containing Strings. The method removes all occurrences of the target String in the chain of nodes and returns a count of how many times that target String was removed.
public static int removeAll(String target, Listnode chain) { //if target is null, throw an IllegalArgumentException Be careful to handle boundary cases, e.g., when the first node and/or last node have Strings that match the target as well as when the chain is empty (has only the header node) or has only one node containing an item. You may assume the following:
-The Listnode class is as described
public class Listnode{ //*** fields *** private E data; private Listnode next; //*** constructors *** // 2 constructors public Listnode(E d) { this(d, null); } public Listnode(E d, Listnode n) { data = d; next = n; } //*** methods *** // access to fields public E getData() { return data; } public Listnode getNext() { return next; } // modify fields public void setData(E d) { data = d; } public void setNext(Listnode n) { next = n; } }
-The Strings can be compared with the equals method.
-The Strings in the chain are guaranteed not to be null.
-The chain is guaranteed to be a correctly formed singly-linked chain of nodes with the next field of the last node set to null.
- A header node is used by the chain of nodes.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
