链表升序排序(降序)

#include<stdio.h>

#include<stdlib.h>

#define N 5

typedef struct node{

   int  data;

   struct node * next;

}ElemSN;

ElemSN  * Createlink(int a[],int n){

       int i;

       ElemSN * h=NULL, * p;

       for( i=N-1;i>=0;i--){

扫描二维码关注公众号,回复: 2531333 查看本文章

              p=(ElemSN *)malloc(sizeof(ElemSN));

      p->data =a[i];

      p->next=h;

      h=p;

        }

return h;

   }

void printlink(ElemSN * h){

          ElemSN * p;

           for(p=h;p;p=p->next)

       printf("%d\n",p->data);

   }

 ElemSN* SelectSont(ElemSN*h)  {

         ElemSN*p,*q,*Pm,*Qm,*h1;   //pq指针联动,Pm最大值指针,Qm最大指针的前一结点 ,h1头结点

         h1=NULL;

while(h){                                //结束条件是头指针为空

   for(Pm=q=h,p=h->next;p;q=p,p=p->next){   

   if(Pm->data>p->data){

         Pm=p;

Qm=q;

   }

   }                                        //for结束,Pm指的是最大值结点

       if(Pm-h)

   Qm->next=Pm->next;    //不是头指针

else

   h=h->next;                      //是头指针

       Pm->next=h1;                    //最大值放在头结点

       h1=Pm;                              //设置头指针

    }

return h1;

}


int main(void){

int a[N]={10,2,80,5,4};

ElemSN * head;

        head=Createlink(a,9);

head=SelectSont(head);

printlink(head);

}



猜你喜欢

转载自blog.51cto.com/13645380/2154300