The movement of the linked list node (maximum value moved to the tail node)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.