链表升序排序(降序)

发布时间:2020-07-03 04:29:13 作者:闫宝通
来源:网络 阅读:1018

#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--){

              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,*h2;   //pq指针联动,Pm最大值指针,Qm最大指针的前一结点 ,h2头结点

         h2=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=h2;                    //最大值放在头结点

       h2=Pm;                              //设置头指针

    }

return h2;

}


int main(void){

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

ElemSN * head;

        head=Createlink(a,9);

head=SelectSont(head);

printlink(head);

}



推荐阅读:
  1. shell数组升序和降序
  2. 链表升序排列(尾插)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

链表 排序 头插

上一篇:MapReduce的典型编程场景3

下一篇:composer插件升级后报错,[ReflectionException] Class Fxp\Composer\AssetPlugin\Repository\Npm

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》