Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please help with this question? I have pasted the provided Node Class and the main java program. All functions have to be recursive.

Can you please help with this question? I have pasted the provided Node Class and the main java program. All functions have to be recursive. image text in transcribed

/* * 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= 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

* Recursive List * * This class extends the Nodes class. * * You are to implement all the missing methods. * (1) Note that ALL these methods are static * (2) EVERY method you implement MUST BE recursive!! * (3) Some of the methods are given in our Lect6.pdf * **************************************************/

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

Question B.4 (25 Points) Recursive Lists. his question deals with singly linked lists. We will use the provided class Node.java which you must not modify. You can run "make run4" to see how it works. We also provided the file RecList.java that extends Node.java. Your task is to implement those methods which have not been implemented yet. Every method that you implement must be recursive! We have provided a main method for testing; please do not modify this main methods. Here is a screen shot of what your code should produce when we run "make r5' hee@c 743] make r /jdk11/bin"/java.exe -classpath bin RecList 111 7 New list: val (0)-5, val(1)-6, val(2)-7, val (3)-13, val (4)-1, val(5)-4, val(6)-4 My print: 5 67 13 1 4 4 My revPrint: 4 4 1 13 7 6 5 size is 7 Value of last-but-one node4 Value of last node4 Index of node with value 4 is 5 GOOD! index is correct Chee@c[744] Question B.4 (25 Points) Recursive Lists. his question deals with singly linked lists. We will use the provided class Node.java which you must not modify. You can run "make run4" to see how it works. We also provided the file RecList.java that extends Node.java. Your task is to implement those methods which have not been implemented yet. Every method that you implement must be recursive! We have provided a main method for testing; please do not modify this main methods. Here is a screen shot of what your code should produce when we run "make r5' hee@c 743] make r /jdk11/bin"/java.exe -classpath bin RecList 111 7 New list: val (0)-5, val(1)-6, val(2)-7, val (3)-13, val (4)-1, val(5)-4, val(6)-4 My print: 5 67 13 1 4 4 My revPrint: 4 4 1 13 7 6 5 size is 7 Value of last-but-one node4 Value of last node4 Index of node with value 4 is 5 GOOD! index is correct Chee@c[744]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Image Databases Towards Advanced Image Retrieval

Authors: Yihong Gong

1st Edition

1461375037, 978-1461375036

Students also viewed these Databases questions

Question

Differentiate between a system flowchart and a program flowchart.

Answered: 1 week ago