Question

Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list...

Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list and returns a reference to the new list. For our example list the statement values = remove(6, values); would result in values referencing the list containing 3 9 12 15 18 19 19 and 20. If target is not contained in list then the list remains unchanged. Please also add a driver class to demonstrate that the method is working correctly.

LLNode.java

//----------------------------------------------------------------------------
// LLNode.java by Dale/Joyce/Weems Chapter 2
//
// Implements <T> nodes for a Linked List.
//----------------------------------------------------------------------------
package support;

public class LLNode<T>
{
protected LLNode<T> link;
protected T info;

public LLNode(T info)
{
this.info = info;
link = null;
}

public void setInfo(T info){ this.info = info;}
public T getInfo(){ return info; }
public void setLink(LLNode<T> link){this.link = link;}
public LLNode<T> getLink(){ return link;}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

package support;

public class Test {

      // recursive method to remove all occurrences of target from list, if exists

      static <T> LLNode<T> remove(T target, LLNode<T> list) {

            // if list is null, returning null (base condition)

            if (list == null) {

                  return null;

            }

            // if current node has target value, ignoring it, and calling recursive

            // method passing target and next node and returning the result

            if (list.getInfo().equals(target)) {

                  return remove(target, list.getLink());

            }

            // creating a Node with this node's value

            LLNode<T> node = new LLNode<T>(list.getInfo());

            // linking the result of recursive remove operation,passing

            // target and next node

            node.setLink(remove(target, list.getLink()));

            // returning resultant node list

            return node;

      }

      // recursive method to print values of a node chain (helper method just for

      // demontration)

      static <T> void print(LLNode<T> list) {

            if (list != null) {

                  System.out.print(list.getInfo());

                  if (list.getLink() != null) {

                        System.out.print(" => ");

                  } else {

                        System.out.println();

                  }

                  print(list.getLink());

            }

      }

      public static void main(String[] args) {

            // creating an array of numbers

            int numbers[] = { 1, 2, 2, 3, 5, 6, 7, 2, 1, 5, 2 };

            // adding first number from array as root value

            LLNode<Integer> nodeList = new LLNode<Integer>(numbers[0]);

            // taking a copy to the head of list

            LLNode<Integer> temp = nodeList;

            // adding each number from the array to the end of node chain

            for (int i = 1; i < numbers.length; i++) {

                  temp.setLink(new LLNode<Integer>(numbers[i]));

                  temp = temp.getLink();

            }

            //displaying node chain

            print(nodeList);

            //removing all occurrences of 2

            System.out.println("removing all occurrences of 2");

            nodeList = remove(2, nodeList);

            //printing node chain

            print(nodeList);

      }

}

/*OUTPUT*/

1 => 2 => 2 => 3 => 5 => 6 => 7 => 2 => 1 => 5 => 2

removing all occurrences of 2

1 => 3 => 5 => 6 => 7 => 1 => 5

Add a comment
Know the answer?
Add Answer to:
Write a recursive method remove(int target, LLNode list) that removes all occurrences of target from list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...

    In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty. The "LLNode" class is given: public class LLNode { protected T info; protected LLNode link; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode...

  • Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T...

    Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T info; private LinkListNode link; public LinkListNode ( T info) this, info = info; link = null; public T getInfo() return info; public LinkListNode getlink() return link; public void setInfo( T info) this.infoinfo; public void setLink (LinklistNode link) this.link = link; public interface StackInterface <T T top() throws StackUnderflowException; void pop () throws StackUnderflowException; void push (T element);

  • Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info...

    Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T) Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: setInfo Access modifier: public Parameters: info (data type T) Return type: void Task: sets the...

  • use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier:...

    use intellij idea main java Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: Queue Node<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name:...

  • Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and...

    Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and false otherwise. For our example list contains(15, values) would return true while contains(10, values) would return false. (JAVA language)

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Write a static recursive method that removes all occurrences of the element from an array list....

    Write a static recursive method that removes all occurrences of the element from an array list. public static void removeAllOccurrences(ArrayList<Integer> list, Integer element)

  • In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list...

    In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT