Question: Implement the methods in RecList recursively.... /* * Node Class -- for singly linked list * * -- randomness is built in (with Random rg)

Implement the methods in RecList recursively....

/* * Node Class -- for singly linked list * * -- randomness is built in (with Random rg) * -- getList(n) returns a random list of length n * * Hw4: You must use this class, but DO NOT MODIFY IT! **************************************************/ import java.util.Random;

class Node { //////////////////////////////// MEMBERS: int val; Node next; static Random rg = new Random(); static int range = 100; // range for generating values //////////////////////////////// CONSTRUCTORS: Node(){ val = rg.nextInt(range); } Node( int v){ val = v; } //////////////////////////////// METHODS: //--------------------------- recursive way to show a list void show (int max){ if (max<=0) max = Integer.MAX_VALUE; show(max, max); }//show(m) // Helper method: void show (int max, int left){ // assume max >= left >= 0 if (left ==0) return; if (next ==null) { System.out.printf("val(%d)=%d. ", max-left, val); return; } System.out.printf("val(%d)=%d, ", max-left, val); next.show(max, left-1); }//show(m,n) //---------------------------recursive way to get a random list public static Node getList(int n) { if (n <= 0) return null; Node u = new Node(rg.nextInt(range)); u.next = getList(n-1); return u; }//getList(n) ///////////////////////////////////////////// //////////////////////////////// MAIN METHOD: public static void main(String[] args){ Node u = Node.getList(7); System.out.printf(" New list: "); u.show(0); }//main }//class

// ==================================================

/* * Recursive List * * This class extends the Nodes class. * * You are to implement all the missing methods. **************************************************/

import java.util.Random;

class RecList extends Node { ////////////////////////////////MEMBERS: ////////////////////////////////CONSTRUCTORS: RecList(){// you do not need to implement anything here super(); } ////////////////////////////////METHODS: static void print(Node u) { // do it! }

static void revPrint(Node u) { // do it! }

static int size(Node u) { // do it! return 0; }

// we implemented it for you static Node add(Node u, int value){ Node v = new Node(value); if (u != null) v.next = u; return v; } // we implemented it for you static Node addUnique(Node u, int v){ if (find(u, v) == null) return add(u,v); return u; } static Node find(Node u, int v){ // do it! return null; } static int findIndex(Node u, int v){ // do it! return 0; } // nth_Node(u, n) will return the n-th node starting from u. // nth_Node(u, 0) returns u. static Node nth_Node(Node u, int n){ // do it! return null; }

//////////////////////////////// MAIN: //////////////////////////////// Please do not change this method! //////////////////////////////// public static void main(String[] args){ int ss = (args.length>0)? Integer.parseInt(args[0]) : 111; int nn = (args.length>1)? Integer.parseInt(args[1]) : 8; rg = (ss==0)? new Random() : new Random(ss); range = 2*nn; Node u = Node.getList(nn); System.out.printf(" New list: "); u.show(0); System.out.printf("My print: "); print(u); System.out.printf("My revPrint: "); revPrint(u); int s = size(u); System.out.printf("My size is %d ", s); Node v = nth_Node(u, s-2); // the last-but-one node (assume s>=2) System.out.printf("Value of last-but-one node = %d ", v.val); System.out.printf("Value of last node = %d ", v.next.val); int i = findIndex(u, v.val); System.out.printf("Index of node with value %d is %d ", v.val, i); if (i == s-2) System.out.printf(" ...GOOD! index is correct "); else System.out.printf(" ...Hmmm! index is may be wrong "); }//main

}//class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!