Question: Complete the MyLL class: package lab 0 8 ; public class MyLL { private class Node { public Node ( T val ) { data

Complete the MyLL class:
package lab08;
public class MyLL {
private class Node {
public Node(T val){ data = val; }
public Node(T val, Node next){ data = val; this.next = next;}
public T data;
public Node next;
}
private Node head;
MyLL(){
// fill this in
}
public void Insert(T previous, T value) throws Exception {
// insert a new node with value "value" after the node with value "previous".
}
public void Append(T value){
// add a new node with value "value" at the end of the list. Head is a special case...
}
public boolean Find(T value){
// return true if "value" is in the list, false if not
}
public boolean Remove(T value){
// remove "value" from the list. return true if it was found and removed, false otherwise
}
}
Tests that need to be passed:
package lab08;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
public class MyLLUnitTests {
@Test
public void findWithEmptyList() throws Exception {
var ll = new MyLL();
Assert.assertFalse(ll.Find("hello"));
}
@Test
public void findWithNonEmptyList() throws Exception {
var ll = new MyLL();
ll.Append("goodbye");
Assert.assertFalse(ll.Find("hello"));
}
@Test
public void findWithOneItemList() throws Exception {
var ll = new MyLL();
ll.Append("hello");
Assert.assertTrue(ll.Find("hello"));
}
@Test
public void findWithManyItemList() throws Exception {
var ll = new MyLL();
ll.Append("hello");
ll.Append("there");
ll.Append("General");
ll.Append("Kenobi");
Assert.assertTrue(ll.Find("hello"));
Assert.assertTrue(ll.Find("Kenobi"));
Assert.assertFalse(ll.Find("Anakin"));
}
@Test
public void findThenRemoveThenAdd() throws Exception {
var ll = new MyLL();
ll.Append("hello");
ll.Append("there");
ll.Append("General");
ll.Append("Kenobi");
Assert.assertFalse(ll.Find("Anakin"));
ll.Remove("Kenobi");
Assert.assertFalse(ll.Find("Kenobi"));
ll.Insert("General","Anakin");
Assert.assertTrue(ll.Find("Anakin"));
}
}

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 Programming Questions!