所以我得到這個代碼,據我所知,我不允許改變:此代碼是否通空到moveAfter(),以及如何處理它
public void insertionSort() {
if (head == tail) {
// empty list is sorted
return;
}
Node nextNode = head.next; // start inserting second node
while (nextNode != tail) {
// set insertNode to node to be inserted in this iteration
Node insertNode = nextNode;
// set nextNode to node to be inserted in next iteration
nextNode = nextNode.next;
// find position where insertNode has to be inserted
Node current = insertNode.prev;
while (current != null && insertNode.value.compareTo(current.value) < 0) {
current = current.prev;
}
// insert insertNode after current
insertNode.moveAfter(current);
}
}
我不是很熟悉的鏈接名單但)我可以告訴如果第二while循環的第一次迭代工作那麼這個代碼將空進入moveAfter(到目前爲止的moveAfter()我有:
/**
* Inserts this node after the specified node. If this and the specified node
* refer to the same node in the list, the list remains unchanged. If the
* specified node current is null, then this node is inserted at the head of
* the list.
*
* Precondition: this Node and the specified node are two nodes of this list
* this node and the specified node are not equal to the tail
*
* @param node - the node in this list after which this node is inserted
*/
public void moveAfter (Node node) {
if(this.prev == null && node.next == null){
throw new NoSuchElementException();
}
if(node.prev == null && node.next==null){
throw new NoSuchElementException();
}
if(this == tail){
throw new NoSuchElementException();
}
if(node == tail){
throw new NoSuchElementException();
}
this.prev.next = this.next;
this.next = node.next;
node.next = this;
this.prev = node;
}
}
如果我是正確的,插入排序( )通過null moveAfter()我怎麼能糾正這一點,並重置「當前」爲它原來的價值,如果我不能查nge insertionSort();
*旁註:道歉,如果我的問題很難閱讀或沒有正確地問。我似乎有一個竅門在這個網站上搞砸了他們。
更好的問在http://codereview.stackexchange.com/ –
我甚至都不知道那是我在那裏發佈的東西,如果我沒有得到任何迴應,可能會把它拿下來。 –
如果你不能改變方法,看看你是否可以覆蓋它。 –