先输入n,然后输入n个数,进行排序。
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void ListSort (struct node *head)
{
struct node *p,*q;
int t;
for(p=head->next; p->next; p=p->next)
for(q=p->next; q; q=q->next)
if(p->data>q->data)
{
t=p->data;
p->data=q->data;
q->data=t;
}
}
int main()
{
struct node *head,*p,*q,*t;
int i,n,a;
scanf("%d",&n);
head = NULL;
for(i=1; i<=n; i++)
{
scanf("%d",&a);
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
ListSort(head);
t=head;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
free(p);
return 0;
}