Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Restrictions: You may not change any of the fields, nor the constructor, nor the insertAtFront method. As usual, you may not modify the method headers

Restrictions:

  • You may not change any of the fields, nor the constructor, nor the insertAtFront method.

  • As usual, you may not modify the method headers given to you in any way nor may you change

    the name of the class or the package.

  • You must use recursion to solve the problems. Your code may not contain any loops. Functions that have loops will receive 0 points

package hw8;

import java.util.NoSuchElementException;

public class MyList {

private class MyListNode {

public Item item;

public MyListNode next;

public MyListNode(Item i, MyListNode n) {

item = i;

next = n;

}

}

private MyListNode first;

/**

* Creates an empty list.

*/

public MyList() {

first = null;

}

/**

* Inserts an item at the front of the list

*

* @param item the item to be inserted

*/

public void insertAtFront(Item item) {

MyListNode node = new MyListNode(item, first);

first = node;

}

/**

* Returns the number of items equal to {@code target} in the list

*

* @param target the data item to count

* @return the number of times {@code target} appears in the list

*/

public int count(Item target) {

return countH(first, target);

}

private int countH(MyListNode first, Item target) {

// TODO

return -1;

}

/**

* Returns the data in position {@code index} in the list.

* Note that like arrays the first data item is in position 0.

* @param index the index of the item to get from the list

* @throws NoSuchElementException if the list doesn't have a

* position {@code index}

* @return the data in position {@code index} in the list.

*/

public Item get(int index) {

if (index < 0)

throw new NoSuchElementException();

return getH(first, index);

}

/* Assumes index is not negative */

private Item getH(MyListNode first, int index) {

// TODO

return null;

}

/**

* Constructs a separate copy of a list. Changes to the copy do not affect the original.

* @return the first node of the copy

*/

public MyList copy() {

MyList answer = new MyList();

answer.first = copyH(first);

return answer;

}

/* returns the first node of a copy of the list whose first node is the argument first */

private MyListNode copyH(MyListNode first) {

// TODO

return null;

}

/**

* Constructs a {@code String} representation of the list. The {@code String}

* is a comma separated listing of the {@code String} representations of the items

* in the same order they appear in the list. There are no spaces between items.

* @return a {@code String} representation of the list.

*/

public String convertToString() {

if (first == null)

return "";

return convertToStringH(first);

}

/* Assumes first is not null */

public String convertToStringH(MyListNode first) {

// Hint: for this function, make the base case a list of size 1

// instead of using the empty list as the base case.

// TODO

return null;

}

/**

* Deletes the first occurrence of {@code target} from the list if it is present.

* Does nothing if {@code target} is not present.

*

* @param target the item to be deleted

*/

public void delete(Item target) {

first = deleteH(first, target);

}

/* returns the first node of the modified list */

public MyListNode deleteH(MyListNode first, Item target) {

// TODO

return null;

}

}

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

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions

Question

Explain the importance of nonverbal messages.

Answered: 1 week ago

Question

Describe the advantages of effective listening.

Answered: 1 week ago

Question

Prepare an employment application.

Answered: 1 week ago