作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不明白为什么我的代码没有按应有的方式迭代并打印 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
关于java - 我使用 insertSorted 方法对随机整数进行排序,但现在我的代码没有按应有的方式迭代 25 次,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61567414/
当我运行我的应用程序时,我在控制台中收到一条消息: 2011-11-16 19:17:41.292 Juice[8674:707] Applications are expected to have
我在 JavaScript 中使用了这个语句,但是当我尝试在 TypeScript 项目中使用它时出现错误。它在提示 fetch(...args) const fetcher = (...args)
我是一名优秀的程序员,十分优秀!