#include<stdio.h>
#include<stdlib.h>
struct LB
{
int data;
struct LB *next;
};
struct LB *Create();
void Shu(struct LB *head);
struct LB *Pai(struct LB *L);
int main()
{
struct LB *List=NULL;
printf("请输入链表元素(输入-1回车结束输入):\n");
List=Create();
Shu(List);
List=Pai(List);
Shu(List);
return 0;
}
struct LB *Create()
{
struct LB *head=NULL,*p,*p1;
p=(struct LB *)malloc(sizeof(struct LB));
scanf("%d",&p->data );
p->next =NULL;
while(p->data !=-1)
{
if(head==NULL)
{
head=p;
p1=p;
}
else
{
p1->next =p;
p1=p;
}
p=(struct LB *)malloc(sizeof(struct LB));
scanf("%d",&p->data );
p->next =NULL;
}
return head;
}
void Shu(struct LB *head)
{
struct LB *p;
p=head;
while(p!=NULL)
{
printf("%-3d",p->data );
p=p->next ;
}
putchar('\n');
}
struct LB *Pai(struct LB *L) //从小到大排序
{
struct LB *head,*q,*p,*h;
head=(struct LB *)malloc(sizeof(struct LB));
head->next =L;
int n=0,j=1;
p=L;
while(p!=NULL)
{
n++;
p=p->next ;
}
for(j=1;j<n;j++)
{
q=head;
p=head->next ;
h=p->next ;
while(h!=NULL)
{
if(p->data >h->data )
{
q->next =h;
p->next =h->next ;
h->next =p;
h=h->next ;
p=q->next ;
}
q=q->next ;
p=p->next ;h=h->next ;
}
}
return head->next ;
}