#include <stdio.h>
#include <stdlib.h>
#define N 9
typedef struct node{
int data;
struct node * NEXT;
}elemsn;
elemsn * Createlink (int a[]) { //reverse creating a one-way linked list
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 linkage to find the maximum value, Pmax is the maximum node, Qmax is the largest node of the previous node
Pmax=h;
for (Q=h,p=h->next;p;q=p,p=p->next) { //Find maximum value
if (pmax->data<p->data) {
pmax=p;
qmax=q;
}
}
if (pmax!=h) { //MAX is judged to be a head node, not a chain of broken chains, is a direct return to the head node
qmax->next=pmax->next; //First disconnect the maximum node from the list.
pmax->next=h; //Max node hangs at head node
H=pmax; The //maximum node is returned as a header node.
}
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);
}
Movement of a linked list node (maximum shift to head node)