Question

In: Computer Science

Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...

Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists.

public class LNode
{
private int m_info;
private LNode m_link;

public LNode(int info){
m_info = info;
m_link = null;
}
public void setLink(LNode link){
m_link = link;
}
public LNode getLink(){
  return m_link;
}
public void setInfo(int info){
m_info = info;
}
public int getInfo(){
  return m_info;
}
}

public class LinkedList
{
private LNode head; // a reference to the first node
// in the list
public LinkedList()
{
head = null;
}

// This non-recursive method traverses the list and
// count the total number of nodes and then save it
// in the last node. You can assume that the list has
// at least one node.
// You will receive zero points if recursion is used.
// For example, the following linked list on the left
// has 3 nodes, so after this method is executed, the
// list will be updated to the one on the right.

public void saveCountinLastNode()
{
  // TODO: implement this method
}

// This recursive method computes the sum of all the nodes
// with odd values.
// You will receive zero points if any looping statements
// are used.
public int addOddNodes(LNode node)
{
  // TODO: implement this method
}
}

Solutions

Expert Solution

- I have included a main method just for testing. Please remove it.

Kindly upvote if this helped


Sample output




CODE:


class LNode {
    private int m_info;
    private LNode m_link;

    public LNode(int info) {
        m_info = info;
        m_link = null;
    }
    public void setLink(LNode link) {
        m_link = link;
    }
    public LNode getLink() {
        return m_link;
    }
    public void setInfo(int info) {
        m_info = info;
    }
    public int getInfo() {
        return m_info;
    }
}

public class LinkedList {
    static int sum = 0;
    private LNode head; // a reference to the first node
    // in the list
    public LinkedList() {
        head = null;
    }

    // This non-recursive method traverses the list and
    // count the total number of nodes and then save it
    // in the last node. You can assume that the list has
    // at least one node.
    // You will receive zero points if recursion is used.
    // For example, the following linked list on the left
    // has 3 nodes, so after this method is executed, the
    // list will be updated to the one on the right.

    public void saveCountinLastNode() {
        LNode current = head;
        LNode temp;
        int count = 0;
        while (current != null) {
            count++;
            if (current.getLink() == null) {
                LNode newNode = new LNode(count);
                current.setLink(newNode);
                break;
            }
            current = current.getLink();
        }
    }

    // This recursive method computes the sum of all the nodes
    // with odd values.
    // You will receive zero points if any looping statements
    // are used.
    public int addOddNodes(LNode node) {
        if (node != null) {
            if (node.getInfo() % 2 != 0) {
                //it is odd value
                sum += node.getInfo(); // have to take a static variable sum as in recursion it will get initialized to default value if we do not take static
            }
            addOddNodes(node.getLink());
        }
        return sum;
    }

    public void dis() {
        LNode current = head;
        while (current != null) {
            System.out.print(current.getInfo() + " ");
            current = current.getLink();
        }
    }

    public static void main(String[] args) {
        //LNode ll = new LNode(3);
        LinkedList l = new LinkedList();
        l.head = new LNode(3);
        l.saveCountinLastNode();
        l.dis();
        int sum = l.addOddNodes(l.head);
        System.out.println("Sum is " + sum);

    }

}

Related Solutions

Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
Using the provided ChangeCalculator class, implement the recursive method calculateChange(int) which will dispense change for a...
Using the provided ChangeCalculator class, implement the recursive method calculateChange(int) which will dispense change for a given amount of money. The method will display and return the total number of combinations of quarters, dimes, nickels, and pennies that equal the desired amount and all of the combinations as well. Avoid duplication. If you choose to use a data structure, it must be one that we've covered and you must thoroughly justify why it was the best choice (based on run-time...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Given the following definition of class Aclass:             class Aclass { private char letter;                  
Given the following definition of class Aclass:             class Aclass { private char letter;                         private int value; public Aclass( ) { Letter =  ‘A’;         value = 0;                         }                         public Aclass( char ch, int num) { letter = ch; value = num;                         }                         public int getvalue( ) { return value; } public void print( ) { System.out.println( “letter =” + letter + “\n value =” + value);                         }             } A.   ( 20 pts ) Write the definition of the class named Bclass as...
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive...
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive method to be tail-recursive. public int f8( int[] arr, int index ) { if ( index == -1 ) return 0; if (arr[index] == 2) return 1 + f8( arr, index - 1 ); return f8( arr, index - 1); }
Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT