java - 我得到了我的 insertSorted 方法来对随机整数进行排序,但是现在我的代码没有按预期迭代 25 次,我做错了什么?
问题描述
我不明白为什么我的代码没有按应有的方式迭代和打印 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
}
解决方案
你忘记了插入的一步
// insert "insertItem" into the proper position within the sorted list
public void insertSorted(T insertItem) {
ListNode<T> currentNode = this.firstNode;
ListNode<T> previousNode = null;
print();
// 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.
else if (currentNode == null) {
insertAtBack(insertItem);
return;
} else {
insertAtPreviousNode(previousNode, insertItem);
}
print();
}
// private void insert(T insertItem, ListNode<T> previousNode) {
// previousNode.nextNode = new ListNode(insertItem, previousNode.nextNode);
// }
// insert item at front of SortedList
private void insertAtPreviousNode(ListNode prev, T insertItem) {
ListNode nNode = new ListNode<T>(insertItem);
nNode.nextNode = prev.nextNode;
prev.nextNode = nNode;
}
// 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
{
ListNode oldFirst = firstNode;
firstNode = new ListNode<T>(insertItem);
firstNode.nextNode = oldFirst;
}
}
// insert item at end of SortedList
private void insertAtBack(T insertItem) {
ListNode oldLast = lastNode;
lastNode = new ListNode<T>(insertItem);
oldLast.nextNode = lastNode;
}
, 输出
The list is: 3 8 9 11 13 16 23 25 29 35 37 43 46 48 49 60 67 68 71 75 78 81 82 92 93
推荐阅读
- algorithm - 体育比赛日程表,换对
- python - 在 PE 完整图像中找到数据但在片段中没有?
- django - 在 django orm 中使用 group by 获取累积运行总和
- r - 可以更简洁地表达这些 R 函数吗?cumcsum()
- sql - WHERE 子句在运行时确定,这可以用没有动态 SQL 的 CASE 来完成吗?
- c# - 如果默认添加 DbContext 范围,为什么要添加它?
- python - Tensorflow ValueError:未知激活函数:heaviside
- javascript - 尝试在 React.js 中使用 Uppy 时出错
- django - Django 通过路径传递变量,导致路径重复。如何摆脱那个循环?
- python - 在python中定义存根