Write a Java program of the implementation of a linked list. The program include
ID: 3840723 • Letter: W
Question
Write a Java program of the implementation of a linked list. The program includes lList.java and lListTester.java. Note: in lListTester.java you have to use TDD (test-driven development) to develop the class.
Here is the specification for the lLink class:
<>
lList
+info : string
+nextList : lList
+isEmpty() : bool
+insert()
+nextList() : lList
+lSize() : int
+deleteInfo()
+traverse() : string
The behaviors are as follows:
isEmpty() return true if lList is empty, i.e. info is null and nextList is null.
insert(inInfo) returns nothing. Place inInfo into lList as a new lList at the beginning of the current lList.
nextList() returns lList. If the current list is not empty, it returns a reference to the beginning of the remaining list. If the current list is empty it returns null. If the current list references “G” then this.nextList() would return a reference to “F”. If the current list is empty (/) then this.nextList() would return null.
lSize() returns an integer which counts the number of none-empty sublists in the current list + 1. If this references “G” then this.lSize() would return 3. If this references “F” then this.lSize() would return 2/ If this references / then this.lSize() would return 0.
deleteInfo(outInfo) returns a reference to the start of the list following the information removed if it exists.
traverse() returns a string which is the current natural order of the linked list as a comma separated set of strings.
<>
lList
+info : string
+nextList : lList
+isEmpty() : bool
+insert()
+nextList() : lList
+lSize() : int
+deleteInfo()
+traverse() : string
Explanation / Answer
public class Node<T> {
Node next;
T value;
}
public <T>void add( Node node, T element ) {
Node current = node;
while ( current.next != null ) {
current = current.next;
}
node.next = new Node( element );
}
public <T>void remove( Node node, T element ) {
Node current = node;
while ( current.next != null && current.next.value != element ) {
current = current.next;
}
if ( current.next != null ) {
current.next = current.next.next;
}
}
public class LinkedListTests {
@Test
public void create_an_empty_list( ) {
LinkedList sut = new LinkedList( );
assertTrue( sut.isEmpty( ) );
}
@Test
public void add_an_element_to_the_list( ) {
LinkedList<Integer> sut = new LinkedList<Integer>( );
sut.add( 1 );
assertFalse( sut.isEmpty( ) );
assertTrue( sut.contains( 1 ) );
}
@Test
public void remove_the_first_element_from_the_list( ) {
LinkedList<Integer> sut = new LinkedList<Integer>( );
sut.add( 1 );
assertTrue( sut.contains( 1 ) );
sut.remove( 1 );
assertTrue( sut.isEmpty( ) );
}
@Test
public void remove_the_last_element_from_the_list( ) {
LinkedList<Integer> sut = new LinkedList<Integer>( );
sut.add( 1 );
sut.add( 2 );
assertTrue( sut.contains( 2 ) );
sut.remove( 2 );
assertFalse( sut.contains( 2 ) );
}
}
public class LinkedList<T> {
Node<T> head = null;
public void add( T val ) {
Node node = new Node( val );
if ( head == null ) {
head = node;
return;
}
Node current = head;
while( current.next != null ) {
current = current.next;
}
current.next = node;
}
public void remove( T val ) {
if ( head == null ) {
return;
}
if ( head.val == val ) {
head = head.next;
return;
}
Node current = head;
while( current.next != null ) {
if ( current.next.val == val ) {
current.next = current.next.next;
return;
}
current = current.next;
}
}
protected boolean contains( T val ) {
Node current = head;
while ( current != null ) {
if ( current.val == val ) {
return true;
}
current = current.next;
}
return false;
}
protected boolean isEmpty( ) {
return head == null;
}
private static class Node<T> {
Node next;
T val;
public Node( T val ) {
this.val = val;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.