if (q == head) //插在第一个节点之前
{
head = t;
}
else //p是q的前驱
{
p->next = t;
}
t->next = q; //完成插入举措
//first = first->next;
}
return head;
}
/*
==========================
成果:冒泡排序(由小到大)
返回:指向链表表头的指针
==========================
*/
struct student *BubbleSort (struct student *head)
{
struct student *endpt; //节制轮回较量
struct student *p; //姑且指针变量
struct student *p1,*p2;
p1 = (struct student *) malloc (LEN);
p1->next = head; //留意领略:我们增进一个节点,放在第一个节点的前面,首要是为了便于较量。由于第一个节点没有前驱,我们不能互换地点
head = p1; //让head指向p1节点,排序完成后,我们再把p1节点开释掉
for (endpt = NULL; endpt != head; endpt = p) //团结第6点领略
{
for (p = p1 = head; p1->next->next != endpt; p1 = p1->next)
{
if (p1->next->num > p1->next->next->num) //假如前面的节点键值比后头节点的键值大,则互换
{
p2 = p1->next->next; //团结第1点领略
p1->next->next = p2->next; //团结第2点领略
p2->next = p1->next; //团结第3点领略
p1->next = p2; //团结第4点领略
p = p1->next->next; //团结第6点领略
}
}
}
p1 = head; //把p1的信息去掉
head = head->next; //让head指向排序后的第一个节点
free (p1); //开释p1
p1 = NULL; //p1置为NULL,担保不发生“野指针”,即地点不确定的指针变量
return head;
}
/*
==========================
成果:插入有序链表的某个节点的后头(从小到大)
返回:指向链表表头的指针
==========================
*/
struct student *SortInsert (struct student *head,struct student *node)
{
struct student *p; //p生涯当前必要搜查的节点的地点
struct student *t; //姑且指针变量
if (head == NULL) //处理赏罚空的有序链表
{
head = node;
node->next = NULL;
n += 1; //插入完毕,节点总数加
return head;
} (编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|