我寫了下面的代碼來插入一個整數到一個鏈表,但在一個排序方法。如何在頭部內的特定節點之後插入鏈表? 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}??!
*/
}
}
你需要一個以前的TEM當你旅行的鏈接,加以前=電流;在current = current-> next之前;在temp-> next = current之後;加上previous-> next = temp; –
@JerryChen我已經將你建議的內容添加到了我的摘錄中。你是這個意思嗎?如果是的話,它將如何改變我的頭? – Soce1991
1.空列表2.最小號碼 –