Question
USING JAVA Begin with the linked list package The existing code is designed to avoid cycles. Add a method public void addToCreateCycle(Node nodeToAdd) that allows
USING JAVA
Begin with the linked list package The existing code is designed to avoid cycles.
- Add a method public void addToCreateCycle(Node
nodeToAdd) that allows the user to add a Node in a way that will create a cycle. - Write a boolean method in the list code that determines whether a cycle exits in the list. You do not need to determine where a cycle starts, only whether one is present.
- Write JUnit tests that create a) a list with no nodes; b) a list with nodes but no cycles; c) a list with only one node with its next reference set to itself; d) a list with a cycle whose length (number of nodes involved in the cycle) is odd; e) a list with a cycle whose length is even.
package linkedlist; //adapted from http://.com/a-simple-singly-linked-list-implementation-in-java/ public class Node
// constructor first public Node() { this(null, null); } public Node(T element) { this.element = element; }
public Node(T element, Node
public T getElement() { return element; }
public Node
public void setElement(T element) { this.element = element; }
public void setNext(Node
}
**************************************************************************************************
package linkedlist;
import javax.swing.JOptionPane;
// heavily adapted from http://www.crunchify.com/a-simple-singly-linked-list-implementation-in-java/ public class SinglyLinkedList
public SinglyLinkedList() { head = null; tail = null; size = 0; }
public void addFirst(T element) { Node
public void addFirst(Node
public void add(T element) { Node
public void add(Node
public void removeFirst() { if (head == null) return; Node
head = head.getNext(); temp.setNext(null); size--; }
public void removeLast() { Node
if (size == 0) return; nodeBefore = head;
for (int count = 0; count < size - 2; count++) nodeBefore = nodeBefore.getNext(); nodeBefore.setNext(null); tail = nodeBefore; size--; }
public void remove(T element) { if (size == 0) return; Node
if (currNode == head) { removeFirst(); currNode = head; } else if (currNode == tail) { removeLast(); currNode = tail; } else { Node
}
public void remove(Node
currentNode = head; if (currentNode == nodeToRemove) removeFirst(); currentNode = tail; if (currentNode == nodeToRemove) removeLast();
if (size - 2 > 0) { nodeBefore = head; currentNode = head.getNext(); for (int count = 0; count < size - 2; count++) { if (currentNode == nodeToRemove) { nodeBefore.setNext(currentNode.getNext()); size--; break; }
nodeBefore = currentNode; currentNode = currentNode.getNext(); } } }
public T get(int index) { Node
public Node
public long size() { return size; }
public Node
public boolean contains(T element) { if (head == null) return false; Node
*************************************************************************************************************************
package linkedlist;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class SinglyLinkedListTester {
@Test public void testRemoveFromListWithoutElement() { SinglyLinkedList
@Test public void testRemoveWithFirstElement() { SinglyLinkedList
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started