2016-03-26 7 views
1

こんにちは私はダブルリンクリストにバブルソートを実行する際に問題があります。バブルソートはさまざまなリストタイプで動作するので、正しいと仮定しています。しかし、私はそれを私のダブルリンクされたリストに使用すると、私は無秩序な混乱と重複ノードで終わる。私は、ダブルリンクリストクラスのaddメソッドとremoveメソッドを使用してソートするので、一方または両方が間違っていなければならないと思います。とにかくここに私のコードがあり、どんな助けも大歓迎です。ダブルリンクリスト(バブルソート)のメソッドを追加

マイaddメソッド、私は

public void my_remove_element(int index) throws myException{ 
    if(index < num_items) 
    { 
     myNode<T> current; 
     if(index <= Math.round(num_items/2)) 
     { 
      current = this.head; 
      for(int i = 0; i < index; i++) 
      { 
       current = current.getRight(); 
      } 
      if(current.getLeft() == null) 
       this.head = current.getRight(); 
      else 
      { 
       current.getRight().setLeft(current.getLeft()); 
       current.getLeft().setRight(current.getRight()); 
      } 
      num_items--; 
     } 
     else 
     { 
      current = this.tail; 
      for(int i = 0; i < (num_items-(index+1)); i++) 
      { 
       current = current.getLeft(); 
      } 
      if(current.getRight() == null) 
       this.tail = current.getLeft(); 
      else 
      { 
       current.getRight().setLeft(current.getLeft()); 
       current.getLeft().setRight(current.getRight()); 
      } 
      num_items--; 
     } 
    } 
    //2.2. If the index is a wrong one 
    else 
     throw new myException("Invalid Index. The ADT does not have such an Index Position"); 
} 

そして私のバブルソート、項目で再び同じ私のremoveメソッド、インデックス

public void my_add_element(int index, T element) throws myException{ 
    if(index <= num_items && index > -1) 
    { 
     if (index == num_items){ 
      myNode<T> nodeAtEnd = new myNode<T>(element); 
      nodeAtEnd.setLeft(tail); 
      nodeAtEnd.setRight(null); 
      if(tail != null) 
       tail.setRight(nodeAtEnd); //link the list 

      tail = nodeAtEnd; //now the tail is the new node i added 

      if(head == null)  // if the list has no elements then set the head 
       head = nodeAtEnd; 

      num_items++; 
     } 
     else 
     { 
      myNode<T> current; 
      myNode<T> nodeToInsert = new myNode<T>(element); 
      if(index <= Math.round(num_items/2)) 
      { 
       current = this.head; 
       for(int i = 0; i < index; i++) 
       { 
        current = current.getRight(); 
       } 
       if(current.getLeft() == null) 
       { 
        this.head = nodeToInsert; 
        nodeToInsert.setRight(current); 
        current.setLeft(nodeToInsert); 
        num_items++; 
       } 
       else 
       { 
        current.getLeft().setRight(nodeToInsert); 
        nodeToInsert.setLeft(current.getLeft()); 
        nodeToInsert.setRight(current); 
        current.setLeft(nodeToInsert); 
        num_items++; 
       } 
      } 

      else 
      { 
       current = this.tail; 
       for(int i = 0; i < (num_items-(index+1)); i++) 
       { 
        current = current.getLeft(); 
       } 
       current.getLeft().setRight(nodeToInsert); 
       nodeToInsert.setLeft(current.getLeft()); 
       nodeToInsert.setRight(current); 
       current.setLeft(nodeToInsert); 
       num_items++; 
      } 
     } 
    } 
    else 
     throw new myException("Invalid Index. The ADT does not have such an Index Position"); 

} 

の値に応じていずれかの頭や尾からのリストをトラバースいくつかの異なるタイプのリストにすることができるリスト、例えばarrayList、linkedList、doubleLinkedListバブルソートは、arrayListおよびLinkedListで機能します。

public void bubble_sort(){ 
    for (int i = 0; i < items.my_get_length()-1; i++) 
     for (int j = 0; j < ((items.my_get_length() - 1) - i); j++) 
     { 
      if (items.my_get_element(j+1).smaller(items.my_get_element(j))) { 
       items.my_add_element(j+2, items.my_get_element(j)); 
       items.my_remove_element(j); 
      } 
     } 
} 

には、サッカー選手の詳細(名前、得点)のリストが含まれています。 これは私が使用するリストです。

名:ルーニー、目標:30
名:イブラヒモビッチ、目標:46
名:メッシ、目標:80
名:アグエロ、目標:21
名:ロナウド、目標:89
名前:ミュラー、目標:33
名:レヴァンドフスキ、目標:30

そして、これはそれがバブルソート

名の後に次のようになります。ポンドrahimovic、目標:46
名:メッシ、目標:80
名:イブラヒモビッチ、目標:46
名:イブラヒモビッチ、目標:46
名:Lewandowskiの、目標:30
名:ルーニー、目標:30
名:アグエロ、目標:21

答えて

0

UPDATE

こんにちは、私はそれを考え出したすべて。インデックスが0または= num_itemsの場合、私のremoveメソッドはポインタを更新していません

関連する問題