2016-12-02 24 views
0

我覺得我非常相信我在這段代碼中是正確的。從邏輯上講,這對我來說很合理,但由於某種原因,程序拒絕跑過某個點。我應該這樣做,而不使用專有類或哈希表。我的列表節點是一個基本的單向鏈表。假設我最初有一個虛擬列表,0,我可以將一個數字添加到列表中,但僅此而已。這是除了添加第一個數字之外無法使用的方法。在java中插入一個數字並保留它的排序

假設我的名單是0 - > 2。和我試圖加1

public void insert(int newElement) { 
    List marker = head; 
    List temp = new List(newElement, null); 

    if (head.next == null) { 
     head.next = temp; 
    } else { 
     while (marker.next != null) { 
      if (newElement < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       marker = marker.next; 
      } 
     } 
    } 
} 
+2

如果你的頭5然後加0會發生什麼,你會得到5-> 0 –

+0

列表,以便馬上蝙蝠我能想到的是不與您的代碼工作的情況。您不會考慮插入的節點可能小於頭部。這立即意味着您的列表變爲未分類。解決這個問題,如果你有更多的問題回來問這些問題。 – Jay

+0

如果有幫助,這完全是您的邏輯問題,而不是與Java本身有關的微妙或錯誤。還沒有提到的一些直接問題是,如果'newElement> = marker.next.value','while'循環永遠不會結束,並且一個元素可能被嘗試插入多次,因爲它在完成一次後不會停止。 –

回答

0
List marker = head; 
    List temp = new List(newElement, null); 

    if (head.value > temp.value) { 
     head.next = new List(head.value, null); 
     head.value = newElement; 
    }else if(head.next == null) 
    { 
     head.next = temp; 
    } 
    else { 
     while (marker.next != null) { 
      if (newElement < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       break; //we added the node, no need to continue looping 
      } 
      else //we need to iterate to the next node in the list until empty 
      { 
       marker = marker.next; 
      } 
     } 
    } 
+0

我試過用這個。看來我仍然陷入困境。我增加了任何價值,它的工作原理。但是,除此之外的任何價值似乎都沒有註冊。 – Shaun

0
public void insert(int val) { 
    Item item = new Item(val); 

    // the case when there is no item (not counting the dummy head) 
    if (head.getNext() == null) { 
     head.setNext(item); 
     item.setNext(null); 
    } else { 
     // if there is at least one item.... 
     Item cursor = head.getNext(); 
     Item prev = cursor; 

     // simply keep looping the list until the new value is less than a value in list 
     // if the new value is greater than all the values in the list... 
     // then the do-while loop will break when null is reached... 
     // at the end of the list 
     do { 
      if (val < cursor.getVal()) { 
       // break and insert 
       break; 
      } 
      prev = cursor; 
      cursor = cursor.getNext(); 
     } while (cursor != null); 

     // insert the item 
     item.setNext(cursor); 

     // the case when the new value is the smallest and is to be inserted at head 
     if (val < head.getNext().getVal()) { 
      head = item; 
     } else prev.setNext(item); 
    } 
} 

這是你的代碼片段:

if (newElement < marker.next.value) { 
    temp.next = marker.next; 
    marker.next = temp; 
    marker = marker.next; 
} 

乘坐鉛筆和紙,並追查這段代碼。你會看到它有什麼問題。請回答look at this並查看附加的圖像。這就是你應該如何真正調試關於鏈表的問題。該圖像不是特定於您的代碼,但它應該讓您知道如何解決這類問題。

0

這似乎是工作,我目前正在測試它的結果很好。任何人都可以找到我錯過的這段代碼的問題嗎?

public void insert(int newElement) { 
    List marker = head; 
    List temp = new List(newElement, null); 

    if (head.next == null) { 
     head.next = temp; 
    } else { 
     for (marker = head; marker.next != null; marker = marker.next) { 
      if (temp.value < marker.next.value) { 
       temp.next = marker.next; 
       marker.next = temp; 
       break; 
      } 

     } 
     if (marker.next == null && temp.value > marker.value) { 
       marker.next = temp; 

      } 
    } 

} 
+0

按順序輸入這些值並打印輸出:'3,4,2,4,1'' – rafid059

+0

輸入3,4,2,4,1,結果很好。非常感謝! – Shaun

+0

它不會添加4次,但這僅僅是因爲我沒有解釋它是否與列表中已有的東西相同,但我確信我可以從這裏得到它! – Shaun