"Data Structure" 2.6 single-linked list application example

Source: Internet
Author: User

1 //single-linked list inversion (head interpolation, time complexity O (n))2 /*Algorithm Ideas:3 Take each node in the original linked list, and insert it as the first node in the original linked list each time, and insert the order and the node with the interpolation method .4 The order is reversed, so the inverted operation can be done. 5 */6 voidReverselist (linklist h)//Reverse: reverse, reverse, reverse7 {8Lnode *p;9p = h->next;//*p points to the first Data element nodeTenH->next = NULL;//set the original linked table to an empty table One      while(P) A     { -Q =p; -p = p->Next; theQ->next = h->next;//inserts the first node behind the current node (that is, the current node is inserted behind the head node) -H->next =Q; -     } - } +  - //deletion of duplicate nodes (nodes with the same data field values in a single-linked list are called repeating nodes, time complexity O (n*n)) + /*Algorithm Ideas: A use pointer p to point to the first Data node, starting from its immediate successor to the end of the list, find the node with the same value and delete it; p Point to at the next node, repeating the above actions, and so on; the algorithm ends when p points to the footer node.  - */ - voiddel_linklist (linklist h) - { -Lnode *p, *q, *R; -p = l->next;//p points to the first node in     if(p = NULL)return 0; -      while(q->next) to     { +Q =p; -          while(Q->next)//finding duplicate nodes from the subsequent start of P the         { *             if(Q->next->data = = p->data) $             {Panax Notoginsengr = q->next;//R points to the repeating node -Q->next = r->Next; the                  Free(r);//Delete R +             } A             Else theQ = q->Next; +         } -p = p->next;//P refers to the next node $     } $ } -  - //single-linked list merging (head interpolation, Time complexity O (m+n), merging: merge) the //two single-linked list A, B elements are ascending order, now a, b merge a by element value non-increment (allow the same value) ordered single-linked list C, do not need to re-apply the node.  - /*Algorithm Ideas:Wuyi take advantage of the characteristics of a, B increment order, take out the current node to compare, remove the current value smaller, insert C's head; the head interpolation, the first to find the minimum node will be at the end of C, and so on, so the resulting c is a non-incremental ordered single-linked list.  - */ WuLinklist merge_linklist (linklist A, linklist B)//A, B are the single linked list of the leading nodes - { About linklist C; $Lnode *p, *q, *R; -p = a->Next; -Q = b->Next; -c = A;//head node of C AC->next =NULL; +      Free(B);//release the head node of B the      while(P &&q) -     { $         if(P->data < q->data) the         { ther = P; p = p->Next; the         } the         Else -         { inR = q; Q = q->next;//Remove a node with a smaller value from the original A and B theR->next = c->next;//inserts the first node behind the current node (that is, the head inserted into C) theC->next =R; About         } the         if(p = = NULL) p =Q; the          while(p)//Remove the remaining nodes and insert the head of C the         { +r = P; p = p->Next; -R->next = c->Next; theC->next =R;Bayi         } the     } the } -  - //the representation of a unary polynomial and the complexity of the addition time O (m+n) the /*Mathematically, an n-ary polynomial can be expressed as a PN (x) =p0+p1*x+p2*x^2+...+pn*x^n, which is uniquely determined by the n+1 coefficients.  the in the computer, it can be represented by a linear table p, i.e. p= (p0,p1,p2,... PN), and the exponent I of each item is implied in the ordinal of its coefficient pi. Similarly, the The QM (x) is expressed as q= (q0,q1,q2,... Qm), M<n is set, and the additive result RN (x) is expressed as r= (p0+q0,p1+q1,p2+q2,..., pm+qm,pm+1,..., pn).  the For efficient and reasonable use of storage space, all items with a factor of 0 are not stored, only the coefficients of non-0 items and their corresponding exponents are stored.  - Algorithm Ideas: the The PA and PB respectively are two participating polynomial head pointers, the PC is the result chain head pointer, the pointer ha and HB respectively point to the polynomial PA and PB in the current comparison node; the Compare the index entries of two nodes to do the following.  the (1) The index value of the node that the pointer ha refers to is less than the exponential value of the node referred to by the pointer HB, and the node of HA is inserted into the tail of the PC chain, and the pointer ha moves a node;94 (2) The exponential value of the node of the pointer ha is equal to the exponential value of the node referred to by the pointer HB, and the coefficients of HA and HB two nodes are added, and if the sum of the coefficients is zero, the two nodes are deleted; the If the sum of the coefficients is nonzero, the sum of the two coefficients is assigned to the node of HA and inserted at the end of the PC chain, and the node of HB is deleted; the (3) The index value of the node that the pointer ha refers to is greater than the exponential value of the node referred to by the pointer HB, and the HB node is inserted into the tail of the PC chain, and the pointer HB is moved to a node.  the if a linked list in PA and PB is compared first, then the remainder of the list that is not compared can be connected directly to the footer of the PC linked list. 98 */ About //Defining Nodes -typedefstructLnode101 {102     floatCoef//coefficient (coeficient)103     intExp//index (exponent)104     structLnode *Next; the}lnode, *linklist;106 //polynomial add-ons107 voidADD (Lnode *pa, Lnode *PB, Lnode *PC)108 {109Lnode *ha, *HB, *temp; the     intsum;111Ha = pa->Next; theHB = pb->Next;113PC = PA;//The PC pointer is initially the header that points to the result chain the      while(Ha! = NULL && HB! =NULL) the     { the         if(Ha->exp < hb->exp)117         {118Pc->next = ha; PC = pc->next; Ha = ha->next;//the PC becomes the move pointer at this point, pointing to the current node of the result chain119         } -         Else if(Ha->exp = = hb->exp)121         {122sum = Ha->coef + ha->Coef;123             if(Sum! =0)//if the coefficients and not zero124             { theHa->coef =sum;126Pc->next = ha; PC = pc->next; Ha = ha->Next;127temp = HB; HB = hb->next; Free(temp); -             }129             Else                    //if the coefficients and zeros are removed, the node ha and HB are deleted, and the two pointers point to the next node respectively the             {131temp = ha; Ha = ha->next; Free(temp); thetemp = HB; HB = hb->next; Free(temp);133             }134         }135         Else                        //ha=>exp > hb->exp;136         {137Pc->next = HB; PC = pc->next; HB = hb->Next;138         }139     } $     if(ha! = NULL)//connect the remaining nodes in the polynomial PA to the PC141Pc->next =ha;142     Else                            //connect the remaining nodes in the polynomial PB to the PC143Pc->next =HB;144PC = PA;//pc pointer reverts to point to result link header145          Free(PB);//release the remaining PB head pointers146}

"Data Structure" 2.6 single-linked list application example

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.