Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

for java please. None of the methods should modify the list, unless that is the purpose of the method. * * You may not add

for java please. None of the methods should modify the list, unless that is the purpose of the method.
 *   * You may not add any fields to the node or list classes.  * You may not add any methods to the node class.  * You may not create any new node objects or other objects.  * For example, you cannot create a new Stack or ArrayList.  *   * Each function must be independent: you cannot call one solution function from the other.   * You MAY add private methods to the list class (helper functions for the recursion).   */
image text in transcribed
image text in transcribed
None of the methods should modify the list, unless that is the purpose of the method.
*
* You may not add any fields to the node or list classes.
* You may not add any methods to the node class.
* You may not create any new node objects or other objects.
* For example, you cannot create a new Stack or ArrayList.
*
* Each function must be independent: you cannot call one solution function from the other.
* You MAY add private methods to the list class (helper functions for the recursion).
*/
public class MyLinked1 {
static class Node {
public Node (double item, Node next) { this.item = item; this.next = next; }
public double item;
public Node next;
}
Node first;
// write a function to compute the size of the list, using a loop
// empty list has size 0
public int sizeLoop () {
return StdRandom.uniform (100); //TODO: fix this
}
// write a function to compute the size of the list, using a forward recursion
// empty list has size 0
public int sizeForward () {
return StdRandom.uniform (100); //TODO: fix this
}
// write a function to compute the size of the list, using a backward recursion
// empty list has size 0
public int sizeBackward () {
return StdRandom.uniform (100); //TODO: fix this
}
// compute the position of the first 5.0 in the list, counting as an offset from the beginning.
// if 5.0 is the FIRST element, the position is 0
// if 5.0 does not appear, return a negative number
// you can write this using a loop or recursion, in any style, but you should only have one loop or recursive helper
// I would expect
// [0,1,2,5,5,5,5,5,8,9].positionOfFirstFiveFromBeginning() == 3
// [0,1,2,5,5,5,5,5,8,9].positionOfLastFiveFromEnd() == 2
public int positionOfFirstFiveFromBeginning () {
return StdRandom.uniform (100); //TODO: fix this
}
// compute the position of the last 5.0 in the list, counting as an offset from the end.
// if 5.0 is the LAST element, the position is 0
// if 5.0 does not appear, return a negative number
// you can write this using a loop or recursion, in any style, but you should only have one loop or recursive helper
// Hint: One way to do this is to use a backwards recursion.
// If the number does appear return a non-negative number.
// If the number does not appear, return the distance to the END of the list as a NEGATIVE number.
// For example:
// [].positionOfLastFiveFromEnd() == -1
// [41].positionOfLastFiveFromEnd() == -2
// [31 41].positionOfLastFiveFromEnd() == -3
// [21 31 41].positionOfLastFiveFromEnd() == -4
// [11 21 31 41].positionOfLastFiveFromEnd() == -5
// [5 11 21 31 41].positionOfLastFiveFromEnd() == 4
// [5 5 11 21 31 41].positionOfLastFiveFromEnd() == 4
// [1 5 5 11 21 31 41].positionOfLastFiveFromEnd() == 4
//
// [].positionOfLastFiveFromEnd() == -1
// [5].positionOfLastFiveFromEnd() == 0
//
// [].positionOfLastFiveFromEnd() == -1
// [41].positionOfLastFiveFromEnd() == -2
// [31 41].positionOfLastFiveFromEnd() == -3
// [5 31 41].positionOfLastFiveFromEnd() == 2
// [21 5 31 41].positionOfLastFiveFromEnd() == 2
// [5 21 5 31 41].positionOfLastFiveFromEnd() == 2
public int positionOfLastFiveFromEnd () {
return StdRandom.uniform (100); //TODO: fix this
}
// delete the first element
public void deleteFirst () {
// TODO
}
public static void main (String args[]) {
//mainDebug ();
mainRunTests ();
}
private static void mainDebug () {
// Use this for debugging!
// Add the names of helper functions if you use them.
Trace.drawStepsOfMethod ("sizeLoop");
Trace.drawStepsOfMethod ("sizeForward");
Trace.drawStepsOfMethod ("sizeBackward");
Trace.drawStepsOfMethod ("positionOfFirstFiveFromBeginning");
Trace.drawStepsOfMethod ("positionOfLastFiveFromEnd");
Trace.drawStepsOfMethod ("deleteFirst");
Trace.run ();
// TODO: Put the test here you want to debug:
testSizeLoop (4, "11 -21.2 31 41");
}
W write a function to compute the size of the list, using a forward recursion // empty list has size public int sizeForward return StdRandon.uniform(100); // TODO fix this W write a function to compote the size of the list, using a backward recursion //epty list has side - public int sizeBackward) return StdRandon.uniform(100)//T000 for this W compute the position of the first 5.0 in the list, counting as an offset from the Wir 5.e is the FIRST element, the position is Wir 5. does not appear, return a negative number w you can write this using a loop or recursion, 10 any style, but you should only be W I would expect W 10,1,2,5,5,5,5,5,8,9). position FirstFiveFromBeginning) - 110,1,2,5,5,5,5,5,8,9). positionofastFiveFrond) - 2 public int positionofFirstFiveFronbeginning return StdRandom uniform (200) = 1/7000: fix this > W compute the position of the last 5.0 in the list, counting as an offset from the // lr 5.0 is the LAST element, the positions // if 5.0 does not appear, return a negative number w you can write this using a loop or recursion in any style, but you should only to X Hint One way to do this is to use a backwards recursio. If the number does appear return a non-begative number 11 If the number does not appear, return the distance to the END of the list as For example: 1.positoreastFiveFront) 1.position Lastiveronnol 131 41.position ofLastFiveFronEod) - 3 (21 31 411.positioflastFiverrand) - (11 21 31 411.positionofas tiverond) = -5 15 11 21 31 41).position LastFivefrond) - 4 15 5 11 21 31 41.positionerlast FiveFromEnd) - 4 1155 11 21 31 41).position LastFiveFromendf) 1.positi onorastFiveFronEnd) 15).positioneftastFiveFrond 11.positionorLastFiveFrond() 1.position Lastiverond 731 41).stionorLastFiveFronEnd -- 15 31 41). position last FiveFronEndl) - 121 5 31 41).position Last FiveFronEnd() - 2 15 21 5 31 411.positionerlast FiveFromEnd) - 2 publicist position LastFiveFromEnd return StdRandon.uniform(100); //TODO: Pix this 11 delete the first element public void deleteFirst) // TODO public static void main(String args 11) * mainRunTests( > private static void mainDebug 77 Use this for debugging // Add the names of helper functions if you use then. Trace.drawStepsOfMethod size oop"); Trace.drawStepsOfMethodszefonard Trace. drawSteps of Method ("sizeBackward") Trace, drawStepsOfMethod positionOfFirst Fivefronbeginning Trace.drawStepsOf Method"position LastFiveFront); Trace.drawStepsOfMethod ("deleteFirst"): Trace.run: // TODO: Put the test here you want to dehu testSizeloop. 11-21.2 11 41"); private static void maintenTests ) testSizeLoop (2,"11 21); testSizeLoop (4,"11-21.2 31 41"); testSizeLoop (1, -11"); testsizeLoop (*): testSizeForward 12. "11 21"} testSizeForward 141 tantsinforvard 41 W write a function to compute the size of the list, using a forward recursion // empty list has size public int sizeForward return StdRandon.uniform(100); // TODO fix this W write a function to compote the size of the list, using a backward recursion //epty list has side - public int sizeBackward) return StdRandon.uniform(100)//T000 for this W compute the position of the first 5.0 in the list, counting as an offset from the Wir 5.e is the FIRST element, the position is Wir 5. does not appear, return a negative number w you can write this using a loop or recursion, 10 any style, but you should only be W I would expect W 10,1,2,5,5,5,5,5,8,9). position FirstFiveFromBeginning) - 110,1,2,5,5,5,5,5,8,9). positionofastFiveFrond) - 2 public int positionofFirstFiveFronbeginning return StdRandom uniform (200) = 1/7000: fix this > W compute the position of the last 5.0 in the list, counting as an offset from the // lr 5.0 is the LAST element, the positions // if 5.0 does not appear, return a negative number w you can write this using a loop or recursion in any style, but you should only to X Hint One way to do this is to use a backwards recursio. If the number does appear return a non-begative number 11 If the number does not appear, return the distance to the END of the list as For example: 1.positoreastFiveFront) 1.position Lastiveronnol 131 41.position ofLastFiveFronEod) - 3 (21 31 411.positioflastFiverrand) - (11 21 31 411.positionofas tiverond) = -5 15 11 21 31 41).position LastFivefrond) - 4 15 5 11 21 31 41.positionerlast FiveFromEnd) - 4 1155 11 21 31 41).position LastFiveFromendf) 1.positi onorastFiveFronEnd) 15).positioneftastFiveFrond 11.positionorLastFiveFrond() 1.position Lastiverond 731 41).stionorLastFiveFronEnd -- 15 31 41). position last FiveFronEndl) - 121 5 31 41).position Last FiveFronEnd() - 2 15 21 5 31 411.positionerlast FiveFromEnd) - 2 publicist position LastFiveFromEnd return StdRandon.uniform(100); //TODO: Pix this 11 delete the first element public void deleteFirst) // TODO public static void main(String args 11) * mainRunTests( > private static void mainDebug 77 Use this for debugging // Add the names of helper functions if you use then. Trace.drawStepsOfMethod size oop"); Trace.drawStepsOfMethodszefonard Trace. drawSteps of Method ("sizeBackward") Trace, drawStepsOfMethod positionOfFirst Fivefronbeginning Trace.drawStepsOf Method"position LastFiveFront); Trace.drawStepsOfMethod ("deleteFirst"): Trace.run: // TODO: Put the test here you want to dehu testSizeloop. 11-21.2 11 41"); private static void maintenTests ) testSizeLoop (2,"11 21); testSizeLoop (4,"11-21.2 31 41"); testSizeLoop (1, -11"); testsizeLoop (*): testSizeForward 12. "11 21"} testSizeForward 141 tantsinforvard 41 W write a function to compute the size of the list, using a forward recursion // empty list has size public int sizeForward return StdRandon.uniform(100); // TODO fix this W write a function to compote the size of the list, using a backward recursion //epty list has side - public int sizeBackward) return StdRandon.uniform(100)//T000 for this W compute the position of the first 5.0 in the list, counting as an offset from the Wir 5.e is the FIRST element, the position is Wir 5. does not appear, return a negative number w you can write this using a loop or recursion, 10 any style, but you should only be W I would expect W 10,1,2,5,5,5,5,5,8,9). position FirstFiveFromBeginning) - 110,1,2,5,5,5,5,5,8,9). positionofastFiveFrond) - 2 public int positionofFirstFiveFronbeginning return StdRandom uniform (200) = 1/7000: fix this > W compute the position of the last 5.0 in the list, counting as an offset from the // lr 5.0 is the LAST element, the positions // if 5.0 does not appear, return a negative number w you can write this using a loop or recursion in any style, but you should only to X Hint One way to do this is to use a backwards recursio. If the number does appear return a non-begative number 11 If the number does not appear, return the distance to the END of the list as For example: 1.positoreastFiveFront) 1.position Lastiveronnol 131 41.position ofLastFiveFronEod) - 3 (21 31 411.positioflastFiverrand) - (11 21 31 411.positionofas tiverond) = -5 15 11 21 31 41).position LastFivefrond) - 4 15 5 11 21 31 41.positionerlast FiveFromEnd) - 4 1155 11 21 31 41).position LastFiveFromendf) 1.positi onorastFiveFronEnd) 15).positioneftastFiveFrond 11.positionorLastFiveFrond() 1.position Lastiverond 731 41).stionorLastFiveFronEnd -- 15 31 41). position last FiveFronEndl) - 121 5 31 41).position Last FiveFronEnd() - 2 15 21 5 31 411.positionerlast FiveFromEnd) - 2 publicist position LastFiveFromEnd return StdRandon.uniform(100); //TODO: Pix this 11 delete the first element public void deleteFirst) // TODO public static void main(String args 11) * mainRunTests( > private static void mainDebug 77 Use this for debugging // Add the names of helper functions if you use then. Trace.drawStepsOfMethod size oop"); Trace.drawStepsOfMethodszefonard Trace. drawSteps of Method ("sizeBackward") Trace, drawStepsOfMethod positionOfFirst Fivefronbeginning Trace.drawStepsOf Method"position LastFiveFront); Trace.drawStepsOfMethod ("deleteFirst"): Trace.run: // TODO: Put the test here you want to dehu testSizeloop. 11-21.2 11 41"); private static void maintenTests ) testSizeLoop (2,"11 21); testSizeLoop (4,"11-21.2 31 41"); testSizeLoop (1, -11"); testsizeLoop (*): testSizeForward 12. "11 21"} testSizeForward 141 tantsinforvard 41 W write a function to compute the size of the list, using a forward recursion // empty list has size public int sizeForward return StdRandon.uniform(100); // TODO fix this W write a function to compote the size of the list, using a backward recursion //epty list has side - public int sizeBackward) return StdRandon.uniform(100)//T000 for this W compute the position of the first 5.0 in the list, counting as an offset from the Wir 5.e is the FIRST element, the position is Wir 5. does not appear, return a negative number w you can write this using a loop or recursion, 10 any style, but you should only be W I would expect W 10,1,2,5,5,5,5,5,8,9). position FirstFiveFromBeginning) - 110,1,2,5,5,5,5,5,8,9). positionofastFiveFrond) - 2 public int positionofFirstFiveFronbeginning return StdRandom uniform (200) = 1/7000: fix this > W compute the position of the last 5.0 in the list, counting as an offset from the // lr 5.0 is the LAST element, the positions // if 5.0 does not appear, return a negative number w you can write this using a loop or recursion in any style, but you should only to X Hint One way to do this is to use a backwards recursio. If the number does appear return a non-begative number 11 If the number does not appear, return the distance to the END of the list as For example: 1.positoreastFiveFront) 1.position Lastiveronnol 131 41.position ofLastFiveFronEod) - 3 (21 31 411.positioflastFiverrand) - (11 21 31 411.positionofas tiverond) = -5 15 11 21 31 41).position LastFivefrond) - 4 15 5 11 21 31 41.positionerlast FiveFromEnd) - 4 1155 11 21 31 41).position LastFiveFromendf) 1.positi onorastFiveFronEnd) 15).positioneftastFiveFrond 11.positionorLastFiveFrond() 1.position Lastiverond 731 41).stionorLastFiveFronEnd -- 15 31 41). position last FiveFronEndl) - 121 5 31 41).position Last FiveFronEnd() - 2 15 21 5 31 411.positionerlast FiveFromEnd) - 2 publicist position LastFiveFromEnd return StdRandon.uniform(100); //TODO: Pix this 11 delete the first element public void deleteFirst) // TODO public static void main(String args 11) * mainRunTests( > private static void mainDebug 77 Use this for debugging // Add the names of helper functions if you use then. Trace.drawStepsOfMethod size oop"); Trace.drawStepsOfMethodszefonard Trace. drawSteps of Method ("sizeBackward") Trace, drawStepsOfMethod positionOfFirst Fivefronbeginning Trace.drawStepsOf Method"position LastFiveFront); Trace.drawStepsOfMethod ("deleteFirst"): Trace.run: // TODO: Put the test here you want to dehu testSizeloop. 11-21.2 11 41"); private static void maintenTests ) testSizeLoop (2,"11 21); testSizeLoop (4,"11-21.2 31 41"); testSizeLoop (1, -11"); testsizeLoop (*): testSizeForward 12. "11 21"} testSizeForward 141 tantsinforvard 41

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions