我無法弄清楚如何將元素插入到排序列表中。我是鏈接列表的新手,但仍然遇到麻煩。以下函數將預定義列表和元素作爲參數。我有白人登上了整個事情,但我仍然無法弄清楚。感謝您的幫助。將元素插入排序列表
/*
* function: lst_insert_sorted
*
* description: assumes given list is already in sorted order
* and inserts x into the appropriate position
* retaining sorted-ness.
* Note 1: duplicates are allowed.
*
* Note 2: if given list not sorted, behavior is undefined/implementation
* dependent. We blame the caller.
* So... you don't need to check ahead of time if it is sorted.
*/
void lst_insert_sorted(LIST *l, ElemType x) {
NODE *p = l->front;
NODE *temp;
NODE *current = p;
NODE *prev;
NODE *next;
if (p->val >= x) { // Base Case if
p->val = x;
}
while (p !=NULL) {
prev = current;
temp = prev->next;
next = current->next;
if (next->val >= x) {
temp->val = x;
}
}
return 0;
}
列表是單鏈表嗎? – 2014-12-05 17:38:42