Я не могу понять, почему мой код не повторяется и печатает 25 случайных чисел, как и должно быть. Моя программа должна напечатать 25 случайных чисел в порядке возрастания. Я получаю вывод без ошибок, но я печатаю где-то между 4 и 7 числами в порядке возрастания. Любой совет?
class ListNode<T extends Comparable<T>> {
// package access members; SortedList can access these directly
T data; // data for this node
ListNode<T> nextNode; // reference to the next node in the list
// constructor creates a ListNode that refers to object
ListNode(T object) {
this(object, null);
}
// constructor creates ListNode that refers to the specified
// object and to the next ListNode
ListNode(T object, ListNode<T> node) {
data = object;
nextNode = node;
}
// return reference to data in node
T getData() {
return data;
}
// return reference to next node in list
ListNode<T> getNext() {
return nextNode;
}
} // end class ListNode<T>
// class SortedList definition
public class SortedList<T extends Comparable<T>> {
private ListNode<T> firstNode;
private ListNode<T> lastNode;
private String name; // string like "list" used in printing
// constructor creates empty SortedList with "list" as the name
public SortedList() {
this("list");
}
// constructor creates an empty SortedList with a name
public SortedList(String listName) {
name = listName;
firstNode = lastNode = null;
}
// insert "insertItem" into the proper position within the sorted list
public void insertSorted(T insertItem)
{
ListNode<T> currentNode = this.firstNode;
ListNode<T> previousNode = null;
// Finding the node that has the greater value
while (currentNode != null) {
// if node is greater than the inserted item, break.
if (currentNode.data.compareTo(insertItem) > 0) {
break;
}
previousNode = currentNode;
currentNode = currentNode.nextNode;
}
// If the first nodes value is less than the inserted value, insert at beginning.
if (previousNode == null) {
insertAtFront(insertItem);
return;
}
// If the end of list is reached then add at the end of the list.
if (currentNode == null) {
insertAtBack(insertItem);
return;
}
}
private void insert(T insertItem, ListNode<T> previousNode) {
previousNode.nextNode = new ListNode(insertItem, previousNode.nextNode);
}
// insert item at front of SortedList
private void insertAtFront(T insertItem) {
if (isEmpty()) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode<T>(insertItem);
else // firstNode refers to new node
firstNode = new ListNode<T>(insertItem, firstNode);
}
// insert item at end of SortedList
private void insertAtBack(T insertItem) {
if (isEmpty()) // firstNode and lastNode refer to same object
firstNode = lastNode = new ListNode<T>(insertItem);
else // lastNode's nextNode refers to new node
lastNode = lastNode.nextNode = new ListNode<T>(insertItem);
}
// remove first node from SortedList
public T removeFromFront() throws EmptyListException {
if (isEmpty()) // throw exception if SortedList is empty
throw new EmptyListException(name);
T removedItem = firstNode.data; // retrieve data being removed
// update references firstNode and lastNode
if (firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem; // return removed node data
} // end method removeFromFront
// remove last node from SortedList
public T removeFromBack() throws EmptyListException {
if (isEmpty()) // throw exception if SortedList is empty
throw new EmptyListException(name);
T removedItem = lastNode.data; // retrieve data being removed
// update references firstNode and lastNode
if (firstNode == lastNode)
firstNode = lastNode = null;
else // locate new last node
{
ListNode<T> current = firstNode;
// loop while current node does not refer to lastNode
while (current.nextNode != lastNode)
current = current.nextNode;
lastNode = current; // current is new lastNode
current.nextNode = null;
}
return removedItem; // return removed node data
}
// determine whether list is empty
public boolean isEmpty() {
return firstNode == null; // return true if list is empty
}
// output list contents
public void print() {
if (isEmpty()) {
System.out.printf("Empty %s%n", name);
return;
}
System.out.printf("The %s is: ", name);
ListNode<T> current = firstNode;
// while not at end of list, output current node's data
while (current != null) {
System.out.printf("%s ", current.data);
current = current.nextNode;
}
System.out.println();
}
} // end class SortedList<T>
Мой класс ListTest закодирован как таковой
public class ListTest {
public static void main(String[] args) {
SortedList<Integer> list = new SortedList<>();
SecureRandom rNum = new SecureRandom();
// insert 25 random (between 0 and 99 inclusive) integers into the list
for (int i = 0; i < 25; i++)
// Your job is to modify insertSorted so that it creates a
// sorted list one element at a time.
list.insertSorted(rNum.nextInt(100));
list.print();
} // end class ListTest
}