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

问题描述 投票:0回答:1

我不知道为什么我的代码没有迭代并按原样打印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
}
java sorting iteration compareto
1个回答
0
投票

您忘记了插入的第一步

        // 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
© www.soinside.com 2019 - 2024. All rights reserved.