2011-d-二-1

#include <stdio.h>
#include <stdlib.h>
#define maxSize 100
typedef struct LNode
{
    int data;
    struct LNode *next;
} LNode;

//数组进行排序、递增
void sort(int a[],int n)
{
   int i,j,temp;
   //冒泡法排序
   for(i=n;i>=1;--i)
   {
       for(j=2;j<=i;++j)
       {
           if(a[j-1]>a[j])
           {
             temp=a[j];
             a[j]=a[j-1];
             a[j-1]=temp;
           }
       }
   }
}
//构造链表并输出
void createList(int a[],int n)
{
    LNode *c,*s,*r,*p;
    int i;
    c=(LNode*)malloc(sizeof(LNode));
    c->data=a[1];
    c->next=NULL;
    r=c;
    for(i=2;i<=n;++i)
    {
       s=(LNode*)malloc(sizeof(LNode));
       s->data=a[i];
       s->next=NULL;
       r->next=s;
       r=r->next;
    }
    //输出链表
    p=c;
    for(i=1;i<=n;++i)
    {
        if(p!=NULL)
        {
            printf("%d\n",p->data);
            p=p->next;
        }
    }
}
int main()
{
    //数组下标从1开始
  int arr[maxSize];
  int temp,n=0,flag=1;
  while(flag==1)
  {
      scanf("%d",&temp);
      if(temp!=0)
      {
          ++n;
          arr[n]=temp;
      }
      else flag=0;//停止输入
  }
  //对数组进行排序
  sort(arr,n);
  //构造输出链表
  createList(arr,n);
}