#include<stdio.h>

#include<stdlib.h>

#define N 9

typedef struct node{

int data;

struct node * next;

}ElemSN;

ElemSN * Createlink(int a[]){ //逆向创建单向链表

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("%2d\n",p->data);

}

ElemSN * MoveMaxnodeToHead(ElemSN*h){

ElemSN * Pmax,* p,* Qmax,* q; //pq联动找最大值,Pmax为最大值结点,QMax为最大值结点的前一结点

Pmax=h;

for(q=h,p=h->next;p;q=p,p=p->next){ //找最大值

if(Pmax->data<p->data){

Pmax=p;

Qmax=q;

}

}

if(Pmax!=h){ //最大值判断是不是头结点,不是就断链挂链,是直接返回头结点

Qmax->next=Pmax->next; // 先把最大值结点与链表断开

Pmax->next=h; //最大值结点挂在头结点

h=Pmax; //最大值结点作为头结点返回

}

return h;

}

int main(void){

int a[]={3,2,5,8,4,7,9,6,1};

ElemSN * head;

head=Createlink(a,9);

head=MoveMaxnodeToHead(head);

Printlink(head);

}