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 ≥...
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)
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 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); }
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a...
c++ programming 1.1 Class definition Define a class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data:  Account holder’s name (string)  Account number (int)  Account type (string, check/savings/business)  Balance (double)  Interest rate (double) – store interest rate as a decimal number.  Add appropriate member functions to manipulate an object. Use a static member in the class to automatically assign account numbers. 1.2 Implement...
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT