链表结构体定义
struct Node
{
int v;
Node* next;
};
struct List
{
int num;
Node* head;
Node* rear;
};
List L;
头插法排序
//头插法排序
void Headsort()
{
Node *head=L.head->next;
L.head->next=NULL;
while(head)
{
Node* target;//目标节点
Node* pre_target;//目标节点的前驱
int maxnum=-0x3f3f3f3f;//比较值
Node* temp=head;//临时变量
Node* pre=head;//临时变量的前驱
while(temp)
{
if(temp->v>maxnum)
{
pre_target=pre;
maxnum=temp->v;
target=temp;
}
pre=temp;
temp=temp->next;
}//寻找链表中最大节点 最大节点的头插效果最终是递增排序
if(maxnum>-0x3f3f3f3f)
{
if(target==head)
{
head=head->next;
}//若寻找到的最大节点为头,则头改变
pre_target->next=target->next;
target->next=L.head->next;
L.head->next=target;//将目标节点在原链表跳过 且头插至链表中
}//头插
}
}