Question
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 sp
2. Write a recursive instance method isSorted that takes a Link parameter and determines whether a linked list is sorted in d
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 instance variables. 1. Write a recursive instance method count that takes no parameters and returns an int. Return the number of links that hold the value zero, with the exception that the last link in the list is not included in the count. For example, for the linked list, {(first) 5→0→3→1→0→0 (last)) it would return the value 2.
2. Write a recursive instance method isSorted that takes a Link parameter and determines whether a linked list is sorted in descending order or not (retun a boolean value). 3. Write a recursive instance method named findBad that takes no parameters and returns a reference to a Link. The result of calling findBad is a reference to the first link that has a value greater than the next link's value. If no such link exists return null. For example, for {5→6→7→3→2) it would return a reference to the third link ("7").
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

class Link{
   private Link next;
   private int value;
  
   public Link(Link n , int v) {
       next = n;
       value = v;
   }
   public Link getNext() {
       return next;
   }
   public void setNext(Link n) {
       next = n;
   }
   public int getValue() {
       return value;
   }
   public void setValue(int v) {
       value = v;
   }
}
class LinkList{
   private Link head;
  
   private int countRec(Link node) {
       if(node==null||node.getNext()==null)return 0;
       if(node.getValue()==0)return 1 + countRec(node.getNext());
       else return countRec(node.getNext());
      
   }
   private Link findBadRec(Link node) {
       if(node==null||node.getNext()==null)return null;
       if(node.getValue()>node.getNext().getValue())return node;
       else return findBadRec(node.getNext());
   }

  
   public LinkList() {
       head = null;
   }
   public void append(int data) {
       Link node = new Link(null,data);
       if(head==null) {
           head = node;
           return;
       }
       Link current = head;
       while(current.getNext()!=null)current = current.getNext();
       current.setNext(node);
   }
  
   public void print() {
       Link current = head;
       while(current!=null) {
           System.out.print(current.getValue()+" ");
           current = current.getNext();
       }
       System.out.println();
   }
  
   public int count() {
       return countRec(head);
   }
  
   public Link findBad() {
       return findBadRec(head);
   }
  
}

public class LinkedListt {
   public static void main(String args[]) {
       LinkList list = new LinkList();
       list.append(5);
       list.append(6);
       list.append(7);
       list.append(3);
       list.append(2);
      
       System.out.println(list.findBad().getValue());
      
      
   }
  
   public static boolean isSorted(Link node) {
       if(node==null||node.getNext()==null)return true;
       if(node.getValue()>=node.getNext().getValue())return isSorted(node.getNext());
       return false;
   }
}

Add a comment
Know the answer?
Add Answer to:
Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...
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
  • Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...

    Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...

  • Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy...

    Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases.    * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all...

    Java StringNode Case Study: Rewrite the following methods in the StringNode class shown below. Leave all others intact and follow similar guidelines. The methods that need to be changed are in the code below. - Rewrite the indexOf() method. Remove the existing recursive implementation of the method, and replace it with one that uses iteration instead. - Rewrite the isPrefix() method so that it uses iteration. Remove the existing recursive implementation of the method, and replace it with one that...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to...

    Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to practice recursion. ignment: The assignment requires writing recursive methods for some linked list operations. The use of loops in the recursive methods is strictly prohibited in this assignment. That is, you cannot use for, while, and do-while in the recursive methods you will write. You can only use a loop when you will initiate values for a linked list in the main method. You...

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

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