List Insertion Sort (insertion-sort-list)

Source: Internet
Author: User

Write their own code there are several large use cases have been difficult, the online code most of the problem, the idea is to first put the list of empty tables, and then the list of elements in the loop into the specified position.

Here is a copy of the correct code, but a list of the leading nodes :

void Insertsort (linklist &l) {    lnode *p,*q,*r,*u;    p=l->next; l->next=null;  Empty the table and insert the original linked table nodes into the ordered table one by one    while (p!=null)   {          //When the list is not up to the end and P is the working pointer        r=l;q=l->next;        while (Q!=null&&q->data<=p->data)       {  //Check the insertion position of P node in the list, then Q is the working    pointer            r=q;q=q-> Next;        }        u=p->next;        p->next=r->next;        r->next=p;        P=u; Link the P node into the list, R is the precursor of Q, and U is the next pointer to the node to be inserted    }}   

The subject is a linked list without a head node , modified as follows:

void Insertsort (linklist &l) {ListNode *p,*u,*r,*q,*s;/*h2 to point to the node that needs to be inserted, H3 to point to the previous node of H2 p=l->next; l->next=null;     Empty table, and then insert the original linked table nodes into the ordered table one by one while (P!=null) {//When the list is not up to the end, p is the working pointer r=l;     q=l->next;       if (P->val < l->val) {s=l;       u=p->next;       p->next=l;       L=p;     P=u;         } else {while (q!=null&&q->val<=p->val) {//Check P node insertion position in the list, then Q is the working pointer r=q;       q=q->next;       } u=p->next;       p->next=r->next;       r->next=p; P=u; Link the P node into the list, R is the precursor of Q, and U is the next pointer to the node to be inserted}}.

  

 

List Insertion Sort (insertion-sort-list)

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.