Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA import java.text.DecimalFormat; import java.util.*; import java.io.*; /* * Complete the methods below. * None of the methods should modify the list, unless that is

JAVA

import java.text.DecimalFormat;

import java.util.*;

import java.io.*;

/*

* Complete the methods below.

* 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 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;

public MyLinked1 () { first = null; }

public boolean isEmpty () { return first == null; }

public void add (double item) {

Node newfirst = new Node (item, first);

this.first = newfirst;

}

/* Constructor to create lists from strings */

public MyLinked1 (String s) {

String[] nums = s.split (" ");

for (int i = nums.length-1; i >= 0; i--) {

try {

add (Double.parseDouble (nums[i]));

} catch (NumberFormatException e) { }

}

}

/* ToString method to print */

public String toString () {

StringBuilder result = new StringBuilder ("[ ");

for (Node x = first; x != null; x = x.next) {

double num = x.item;

if (num % 1.0 == 0) {

result.append (String.format("%.0f", num)); // print integers without decimal

} else {

result.append (num);

}

result.append (" ");

}

result.append ("]");

return result.toString ();

}

// 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 an optimistic, 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 an optimistic, 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

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: Use a backward recursion.

// Hint: If the number does not appear, return the distance to the END of the list as a NEGATIVE number.

public int positionOfLastFiveFromEnd () {

return StdRandom.uniform (100); //TODO: fix this

}

// delete the first element

public void deleteFirst () {

// TODO

}

}

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

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

Do you currently have a team agreement?

Answered: 1 week ago

Question

c. How is trust demonstrated?

Answered: 1 week ago