2016-09-19 5 views
-2

私は、リンクリストクラスの昇順で並べ替えるための挿入ソートを作ろうとしています。私は本当に何をする必要があるかわからない。私はリストの先頭に戻る方法を見つけることができません。挿入の並べ替え単一のリンクされたリストのためにJava

public static void LLInsertionSort (LinkedList LL){ 

    IntNode currentNode = head; 
    IntNode tail = null; 
    while(currentNode != null&& tail != head){ 


     while (currentNode.getData() > currentNode.getNext().getData()){ 

      int temp = currentNode.getData(); 
      currentNode.setData(currentNode.getNext().getData()); 
      currentNode.getNext().setData(temp); 
+1

*すっごく実際にはJavaScriptに関連するものだと確信していますが...私たちは死と同じことを言うべきでしょう - 今日ではありません。 – vlaz

+0

あなたのコードは不完全です。また、あなたは変数 'head'を持っていますが、どこから来るのかは不明ですが、なぜそれを使用できませんか?また、なぜノードを再編成するのではなく、ノードのデータを交換しているのですか? – Taylor

答えて

0

あなたはあなたのリストで最初のノードから毎回起動する必要があります。

public static IntList LLInsertionSort(Node head) 
{ 
    IntNode current = head; 
    IntNode tail = null; 
    while(current != null&& tail != head) 
    { 
     IntNode next = current; 
     for(; next.next != tail; next = next.next) 
     { 
     if(next.value <= next.next.value) 
     { 
      int temp = next.value; 
      next.value = next.next.value; 
      next.next.value = temp; 
     } 
     } 
     tail = next; 
     current = head; 
    } 
    return this; 
} 
関連する問題