Question

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 driver application that demonstrates that your new method operates correctly.

16. Write a recursive method numEvens(LLNode list) that returns how many even numbers are contained in list. For our example list numEvens(values) would return 5 (the even numbers are 6, 6, 12, 18, and 20).

17. Write a recursive method contains(int target, LLNode 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.

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

Comments are always hepful

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

A.

int numEvens(llNode list)
{
   if(list==null)
       return 0;
   return (list.data%2==0 ? 1 : 0) + numEvens(list.next);
}

B.

boolean contains(int target, llNode list)
{
   if(list==null || list.data > target)
       return false;
  
   if(list.data==target)
       return true;
   else
       return contains(target, list.next);
}

C.

llNode remove(int target, llNode list)
{
   if(list==null || list.data > target)
       return list;
   if(list.data==target)
       return remove(target, list.next);
   else
   {
       list.next=remove(target, list.next);
       return list;
   }
}

Add a comment
Know the answer?
Add Answer to:
In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked 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
  • 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 //---------------------------------------------------------------------------- //...

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

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

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

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • Write a JAVA contains method for a linked implementation of a sorted list. #This method is...

    Write a JAVA contains method for a linked implementation of a sorted list. #This method is written from the implementation perspective, meaning it would go inside of the LinkedSortedList class. This means we have access to firstNode and numberOfEntries. #write an efficient solution. This will involve directly accessing the linked structure rather than only invoking existing methods. You can assume T is Comparable. #The method header is: public boolean contains(T anEntry)

  • Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a...

    Q2 [15 pts] Linked list: For this question use class Node.java Assume this file implements a node in a linked list. It has the following public data members: ➢ int data ➢ Node next Write class Node and Class single linked list then write the following functions: 2.2 Remove duplicates from an unsorted linked list Write a Java method removeDuplicates() which takes a list and deletes any duplicate nodes from the list. The list is not sorted. Example: if the...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • 117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....

    117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...

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