【文件属性】:
文件名称:链表的排序
文件大小:2KB
文件格式:TXT
更新时间:2015-12-12 04:03:57
链表的排序
# include
#include
struct sort1
{
int a;
struct sort1 *next;
};
struct sort1 *creatsort1(struct sort1 *d,int n);
void printsort1(struct sort1 *l);
struct sort1* creatsort1(struct sort1 *d,int n)
{
struct sort1 *p,*h;
int i,x;
d=(struct sort1*) malloc (sizeof(struct sort1));
p=d;
for(i=1;i<=n;i++)
{
h=(struct sort1*) malloc (sizeof(struct sort1));
scanf("%d",&x);
h->a=x;
p->next=h;
p=p->next;
}
return d;
}
void printsort1(struct sort1 *l)
{
struct sort1 *p;
p=l->next;
printf("%s\n","the data:");
while(p!=NULL)
{
printf("%d ",p->a);
p=p->next;
}
printf("\n" );
}
struct sort1 *sort(struct sort1 *head)
{
struct sort1 *min;
struct sort1 *p_min;
struct sort1 *p;
struct sort1 *first;
struct sort1 *end;
first=NULL;
while(head!=NULL)
{
for(p=head,min=head;p->next!=NULL;p=p->next)
{
if(p->next->aa)
{
p_min=p;
min=p->next;
}
}
if(first==NULL)
{
first=min;
end=min;
}
else
{
end->next=min;
end=min;
}
if(min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (first != NULL&&end;==NULL)
{
head = first;
printsort1(head);
}
}
void main(){
struct sort1 *la;
int n;
printf("输入准备排序数字的个数,例如5!\n");
scanf("%d",&n);
printf("输入 %d 个数字!\n",n);
la=creatsort1(la,n);
sort1(la);
}