【发布时间】:2015-12-07 00:51:13
【问题描述】:
我编写了以下代码来将整数插入到链表中,但采用排序方法。
我评论了我的问题所在并在下面进行了解释:
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 链接,添加previous = current;当前=当前->下一个之前;在 temp->next = current 之后;添加上一个->下一个 = temp;
-
@JerryChen 我已将您的建议添加到我的 sn-ps 中。你是这个意思吗?如果是,它将如何改变我的想法?
-
1.空列表 2. 最小的数
-
@JerryChen 你会解释更多吗?我是这个概念的新手,我比文字更了解代码
标签: c++ linked-list nodes head