单链表插入排序
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插法
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
///插入排序思想
void insertsort(LNode *&L)
{
LNode *p,*q,*r;
p=L->next->next;///插入排序从第二个元素开始
L->next->next=NULL;
while(p)
{
q=p->next;///p接着的元素节点
r=L;
while(r->next!=NULL && r->next->data < p->data)///找p应该插入的位置
r=r->next;
///头插
p->next=r->next;
r->next=p;
p=q;///继续下一个
}
}
int main()
{
LNode *L,*B;
int n,c[10000];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&c[i]);
creathead(L,c,n);
insertsort(L);
while(L->next!=NULL)
{
printf("%d ",L->next->data);
L=L->next;
}
printf("\n");
return 0;
}
来源:CSDN
作者:相似的人适合打闹
链接:https://blog.csdn.net/AYSXY/article/details/104476881