Movement of a linked list node (maximum shift to head 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 * 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)

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.