【发布时间】:2020-09-26 06:07:34
【问题描述】:
这是我的功能:
void IntListInsertInOrder (IntList L, int v)
{
struct IntListNode *n = newIntListNode (v);
if (L->first == NULL) { //case a, empty list
L->first = L->last = n;
L->size ++;
return;
}
else if (v <= L->first->data) { // case b, smallest value
n->next = L->first;
L->first = n;
}
else if (v >= L->last->data) { // case c, largest value
L->last->next = n;
L->last = n;
}
else if (v > L->first->data && v <= L->first->next->data) { // case d, second-smallest value
n->next = L->first->next;
L->first->next = n;
}
else { //case f, value in the middle
struct IntListNode *curr = L->first;
while (curr->next->data < v) {
curr = curr->next;
}
n->next = curr->next;
curr->next = n;
}
L->size ++;
return;
}
当我将 10 个数字的随机列表放入其中时,3/10 排序正确。错误似乎在最后一部分,但它看起来与我在网上找到的解决方案完全一样。
【问题讨论】:
-
在此链接中查看add_ordered() function。 (您的
first和last在链接函数中似乎是head和tail) -
构成不正确列表的插入顺序是什么?举一个例子
-
@Yuyan_Li 显示列表是如何定义的。
标签: c linked-list insert singly-linked-list function-definition