本关任务:补全 insertSort 函数,实现将一个结点按结点数据 data 从小到大的顺序插入到一个有序链表中。根据插入结点 data 的大小,插入点可能是链表头、尾或者中间某个位置。
#include "linearList.h"
node * insertSort(node *h, node *t)
{
if(h == NULL) return t;
node * head = h;
node * p = NULL;
node * q = h;
for(;q;)
{
if(t->data < q->data)
{
if(q == head)head = t;
t->next = q;
q = q->next;
if(p)p->next = t;
p = t;
return head;
}
else p = q, q = q->next;
}
p->next = t;
return head;
}