2015-12-07 32 views
0

我寫了下面的代碼來插入一個整數到一個鏈表,但在一個排序方法。如何在頭部內的特定節點之後插入鏈表? C++

我評論在我的問題是,解釋如下它:

void LLL::insertSorted(int r) { 
    node * temp = NULL; 
    node * current = NULL; 

    if (head == NULL) { 
     head = new node; 
     head->data = r; 
     head->next = NULL; 
    } else { 
     temp = new node; 
     temp->data = r; 
     current = head; 
     while (temp->data > current->data && current != NULL) { 
      current = current->next; 
     } 
     temp->next = current; 
     /* 
     * Assume that I have head points to this list: { 3 -> 5 -> 8 -> NULL } 
     * And I want to insert {6} (temp) to the list just after 5; then what 
     * I've done so far on my previous code I made temp = {6 -> 8 -> NULL}. 
     * NOW!! How can correctly insert temp to ((head)) just after {5}??! 
     */ 
    } 
} 
+0

你需要一個以前的TEM當你旅行的鏈接,加以前=電流;在current = current-> next之前;在temp-> next = current之後;加上previous-> next = temp; –

+0

@JerryChen我已經將你建議的內容添加到了我的摘錄中。你是這個意思嗎?如果是的話,它將如何改變我的頭? – Soce1991

+0

1.空列表2.最小號碼 –

回答

0

你要記住你是後插入的節點,並具有節點鏈接到新的一個。

例如,在循環地發現,來後tempcurrent,有另一個變量,說prev,只是current = current->next之前做pre = current的節點。

+0

你會告訴我如何使用我的代碼片段嗎? – Soce1991

+0

你的意思是讓前一個節點取消引用{5},並使前一個 - >下一個= temp? – Soce1991

0
void LLL::insertSorted(int r) { 
    node * cur = head; 
    node * prev = NULL; 

    while((cur != NULL) && (r > cur->data)){//find the location to insert 
     prev = cur; 
     cur = cur->next; 
    } 

    node *new_node = new node; 
    new_node->data = r; 
    new_node->next = cur; 

    if(prev == NULL){//new first one 
     head = new_node; 
    }else{ 
     prev->next = new_node; 
    } 
} 
+0

這將如何改變我的頭?你能解釋一下嗎? – Soce1991

+0

所以你只需要在列表爲空時或者想要插入的數字最小時更改頭像。如果列表爲空,那麼「new_node」是(num,空),並且我們將這個new_node分配給頭部。否則(當列表不是空的,並且你試圖插入的數字是最小的)時,代碼中的「while循環」部分將不會被執行,因爲num(r)< cur->數據),所以new_node現在是(num,cur (之前的頭)),並且我們將這個新節點分配給頭 –

+0

@ Soce1991以上評論是否幫助您更好地理解? –

0

你需要一個新的節點時插入
我覺得只是讓你想插入一前一後一個新的節點,就像5在您的評論

+0

您是否將此應用於我的代碼,以便我能更好地理解您的意思,並將其標記爲答案,以便其他人可以找到解決方案? – Soce1991

+0

如果您不想將鏈接列表更改爲雙鏈接列表,您可以嘗試將'while(temp-> data> current-> data && current!= NULL)'改爲'while(temp-> data> current-> next-> data && current!=「),這將使得」6「與8比較,而不是5,它將在5之後插入,而不是在8之前,你不需要添加」前一個「節點,只需」下一個「,對不起,我的英文不好 –

+0

你的英文很好,但我更瞭解代碼。所以,如果你可以用我的代碼來解決這個問題,那麼我們不需要英文 – Soce1991

0

這是最後的結果是:(它的工作原理!) 它處理以下情況:

  • 情況1:頭是空的。
  • 情況2:新元素是列表中最小的。
  • 案例3:新元素位於列表中間的某個位置。
  • 案例4:新元素是最大的。

void LLL::insertSorted(int r) { 
 
    node * temp = NULL; 
 
    node * current = NULL; 
 
    node * previous = NULL; 
 

 
    if (head == NULL) { 
 
     head = new node; 
 
     head->data = r; 
 
     head->next = NULL; 
 
    } else { 
 
     temp = new node; 
 
     temp->data = r; 
 
     temp->next = NULL; 
 

 
     current = head; 
 
     if (temp->data < current->data) { 
 
      temp->next = head; 
 
      head = temp; 
 
     } else { 
 
      while (current != NULL && temp->data >= current->data) { 
 
       previous = current; 
 
       current = current->next; 
 
      } 
 
      temp->next = current; 
 
      previous->next = temp; 
 
     } 
 
    } 
 
}

+0

下面的插入函數似乎沒問題。檢查Node和List的構造函數。 –

+0

@JerryChen如果我將循環條件的第二部分更改爲(current-> next!= NULL),它將起作用,但如果我輸入了{1,2,3,4,5},則此次跳過第一個輸入{2 ,3,4,5,1}!你知道什麼是問題嗎?以及如何解決它。 (請用代碼回答,少許英語) – Soce1991