Two addition of polynomial (C + +)

Source: Internet
Author: User

Implement two polynomial to add does not open space (this requires the implementation to add, at the cost of two original linked list will be modified)


Analysis:
This>other put the other current node before this

This<other moves forward one bit on this current node, and the successor also moves forward one

This==other sum of 0 is deleted, and all move forward one bit, not equal to delete the current node in other and move forward.

Attention:

It is important to note that N as always points to Mhead, N->next always points to the next node of the other linked list, so when you modify the other list, you must pay attention to N->next's point

Some books on the C language implementation of the sum of the polynomial, if the modification of the corresponding code embedded in C + +, can get the correct results, but the C + + destructor will be error, so the other table in the pointer n point to the explicit

1 voidLink::add (Node *mhead) {            2Node * ph = head->Next;3Node * pm = mhead->Next;4Node * m = Head;//as a token, mark the last visited node of this5Node * n = mhead;//N always points to the head node, and N->next points to the next node element of PM6 7      while(Ph!=null && Pm!=null) {//determine when a or B two linked list is not empty8         if(Ph->index>pm->index) {//This>other, inserts the first node of other before the this current node9N->next = pm->next;//Let N's next pointer point to the next node of PM,TenM->next =pm; Onem =pm; AM->next =ph; -PM = n->next;//when the PM is inserted above, the PM points to the next node, i.e. N->ENXT -         } the         Else if(Ph->index<pm->index) {//This<other only need to move this node back one bit, note that M is never the last node of this -m =ph; -ph = ph->Next; -         } +         Else{//This==other -Node *tem; +             if(ph->ratio+pm->ratio==0) {//The sum of 0 removes the current node of this and moves back one ATEM =ph; atph = ph->Next; - Delete (TEM); -             } -             Else{//sum not 0, add coefficients -Ph->ratio = ph->ratio+pm->Ratio; -}//when equal, remove the current node from other and move back one inTEM =pm; -PM = pm->Next; toN->next = PM;// + Delete (TEM);  -         } the     } *     if(Ph==null) {//Since the this is moved only after the other >, so the PM is inserted into the null representation, not empty, $M->next = PM;//The list that represents this is empty, so insert the rest of the other list into the footer of this, that is, N points to the last node N->next exactly the footer pointerPanax NotoginsengN->next = NULL;//set the end of the footer to empty -     } the } + voidLink::D elete (Node * tem) {//Delete node tem A     Deletetem; the}

    

Two addition of polynomial (C + +)

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.