2016-07-30 11 views
2

3番目の値をキューに挿入すると、無限ループになります。 Insertメソッドのためのリンクリスト優先度キューの無限ループ

コード:そこので

///////////////////////////////////////////////////////// 
/// BEGIN TESTING LIST IMPLEMENTATION 
///////////////////////////////////////////////////////// 

Current Size: 0 
--- BEGIN TESTING INSERT() --- 
Checking if the list is empty BEFORE insertion... 
0. Is it empty? [ true ] 
1. Is it empty? [ false ] 
2. Is it empty? [ false ] 

public P2Driver() { 
    list = new OrderedListPriorityQueue<TestInteger>(); 
    randomInteger = new Random(8); 
    randomPriority = new Random(8); 

    print(""); 
    print("/////////////////////////////////////////////////////////"); 
    print("/// BEGIN TESTING LIST IMPLEMENTATION"); 
    print("/////////////////////////////////////////////////////////"); 
    print(""); 

    // Test insert 
    printCurrentSize(); 
    print("--- BEGIN TESTING INSERT() --- "); 
    print("Checking if the list is empty BEFORE insertion..."); 
    start = System.currentTimeMillis(); 
    for (int i = 0; i < MAX_SIZE; i++) { 
     print(i + ". Is it empty? [ " + list.isEmpty() + " ]"); 
     list.insert(new TestInteger(randomInteger.nextInt(10))); 
    } //GETS STUCK HERE<<<<<<<<<<<<<<<<< 

が出力:ここ

public boolean insert(E object) { 
    Node<E> Nody = new Node(object); 
    Node<E> current = head; 
    Node<E> previous = null; 
    if (isEmpty()) 
     head = Nody; 
    while (current != null) { 
     if (((Comparable<E>) object).compareTo(current.data) >= 0) { 
      previous = current; 
      current = current.next; 
     } else { 
      if (((Comparable<E>) object).compareTo(current.data) < 0) { 
       previous = Nody; 
       Nody = current; 
      } 
      if (previous == null && ((Comparable<E>) object).compareTo(current.data) < 0) { 
       Nody.next = head; 
       head = Nody; 
      } 
     } 
    } 
    currentSize++; 
    return false; 
} 

は、私が使用するドライバと私は無限ループを得る場所であります存在しないコードです。これは、ドライバがスタックしていることを意味します。私は何が間違っているのか分かりません。

+0

あなたは本当に 'E extends Comparable ' – oldrinb

答えて

1

whileループ内のelseステートメントはwhileループの条件に使用されている現在のものを再定義していないため、そこに滞留しています。

+0

のようなクラス宣言で型パラメータを指定する必要があります。ああ、私の愚かな間違い。あなたは鷹の目を持っています。 – Orcka