gpt4 book ai didi

java - 我使用 insertSorted 方法对随机整数进行排序,但现在我的代码没有按应有的方式迭代 25 次,我做错了什么?

转载 作者:行者123 更新时间:2023-12-01 16:57:28 27 4
gpt4 key购买 nike

我不明白为什么我的代码没有按应有的方式迭代并打印 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com