链表升序排序(降序)
#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);
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。