优先链接队列添加方法帮助

我正在尝试使用链接节点实现优先级队列,并且除了 add 方法之外,我的所有方法都可以正常工作。add 方法的目的是将一个可比较的对象以正确的顺序添加到队列中。队列的顺序如下:优先级最高的节点是firstNode。任何关于我在尝试中做错了什么的帮助将不胜感激。


public void add(T newEntry) {


   if(newEntry == null) {

       return;

   }


   if(isEmpty()) { 

      firstNode = new Node(newEntry);

   } else {

       Node currentNode = firstNode;

       if(newEntry.compareTo(firstNode.data)<0) {

           firstNode = new Node(newEntry, firstNode);

           length++;

           return;

       } else {

           while(currentNode.getNextNode() != null && newEntry.compareTo(currentNode.next.data) > 0) {

                  currentNode = currentNode.next;

                  currentNode.setNextNode(new Node(newEntry, currentNode.getNextNode()));

           }

       }

   }

   length++;

   return;     


陪伴而非守候
浏览 186回答 1
1回答

米琪卡哇伊

您至少有两个问题,我已经在代码注释中指出:public void add(T newEntry) {&nbsp; &nbsp;if(newEntry == null) {&nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp;}&nbsp; &nbsp;if(isEmpty()) {&nbsp;&nbsp; &nbsp; &nbsp; firstNode = new Node(newEntry);&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;Node currentNode = firstNode;&nbsp; &nbsp; &nbsp; &nbsp;if(newEntry.compareTo(firstNode.data)<0) {// Here you're assigning a new value to firstNode, but not linking to the old// firstNode. So you're losing the entire list.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;firstNode = new Node(newEntry, firstNode);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;length++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(currentNode.getNextNode() != null && newEntry.compareTo(currentNode.next.data) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentNode = currentNode.next;// Here you're adding multiple new nodes to the list.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentNode.setNextNode(new Node(newEntry, currentNode.getNextNode()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;length++;&nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp;}你可以很容易地简化它:public void add(T newEntry) {&nbsp; &nbsp;if(newEntry == null) {&nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp;}&nbsp; &nbsp;Node newNode = new Node(newEntry);&nbsp; &nbsp;if(isEmpty()) {&nbsp;&nbsp; &nbsp; &nbsp; firstNode = newNode;&nbsp; &nbsp;} else if (newNode.data < firstNode.data) {&nbsp; &nbsp; &nbsp; // make newNode point to the firstNode,&nbsp; &nbsp; &nbsp; // and then re-assign firstNode&nbsp; &nbsp; &nbsp; newNode.setNextNode(firstNode);&nbsp; &nbsp; &nbsp; firstNode = newNode;&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp;Node currentNode = firstNode;&nbsp; &nbsp; &nbsp; &nbsp;Node nextNode = currentNode.getNextNode;&nbsp; &nbsp; &nbsp; &nbsp;while (nextNode != null && nextNode.data > newNode.data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;currentNode = nextNode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nextNode = currentNode.getNextNode;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;// insert newNode between currentNode and nextNode&nbsp; &nbsp; &nbsp; &nbsp;newNode.setNextNode(nextNode);&nbsp; &nbsp; &nbsp; &nbsp;currentNode.setNextNode = newNode;&nbsp; &nbsp;}&nbsp; &nbsp;length++;&nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java