#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 * Movemaxnodetotail (elemsn*h) {
ELEMSN * pmax,* p,* qmax,* q;
Pmax=h;
for (Q=h,p=h->next;p;q=p,p=p->next) {
if (pmax->data<p->data) {
Pmax=p;
qmax=q;
}
} //for loop out P equals null,q as tail node, Pmax maximum node, Qmax the last node of the maximum node .
if (pmax->next) { //determines if the maximum node is a tail node.
if (pmax!=h) //Determine if the maximum node is a head node.
qmax->next=pmax->next; //Broken chain chain
Else
h=h->next; //Is the head node, and the head node pointer h moves back .
pmax->next=q->next; //Key operation: If the next value node is not null (Q->next equals null), the secondary chain is in place in a one-way circular list.
q->next=pmax; //MAX hook to tail node
}
return h;
}
int main (void) {
int a[]={9,3,5,8,4,7,2,6,1};
ELEMSN * HEAD;
Head=createlink (a,9);
Head=movemaxnodetotail (head);
Printlink (head);
}
The movement of the linked list node (maximum value moved to the tail node)