#include<stdio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
struct link *invent(void);
struct link *sort(struct link *head);
void outp(struct link *head);
int main()
{
struct link *head,*p;
printf("创建一个链表。\n");
head=invent();
p=head;
outp(p);
printf("排序后结果为:\n");
p=sort(head);
outp(p);
return 0;
}
struct link *invent(void)
{
struct link *head=NULL,*tail,*new;
int n=0;
while(1)
{
new=(struct link *)malloc(sizeof(struct link));
printf("请输入第%d个数据,输入-1结束:",n+1);
scanf("%d",&new->data );
new->next =NULL;
if(new->data==-1)
{
free(new);
return head;
}
else
{
n++;
if(n==1)
{
head=tail=new;
}
else
{
tail->next =new;
tail=new;
}
}
}
}
void outp(struct link *head)
{
while(head)
{
printf("%d\n",head->data );
head=head->next ;
}
}
struct link *sort(struct link *head)
{
struct link *p,*q;
int temp=0;
p=head;
while(p!=NULL)
{
q=p;
temp=0;
while(q!=NULL )
{
if(p->data>q->data )
{
temp=p->data ;
p->data=q->data;
q->data=temp;
}
q=q->next ;
}
p=p->next ;
}
return head;
}