The application of linear table in the operation of one-dimensional polynomial

Source: Internet
Author: User

1-1 polynomial operation of experimental problem

"Problem description" has two exponential diminishing unary polynomial, write a procedure to seek the and of the two polynomial, and then seek their product.

"Hint" uses a single linked list with a header node as the storage representation of a polynomial; To create two single-linked lists; The polynomial addition is to insert a node in a single linked list into another single linked list, pay attention to the correct modification of pointers in insert and delete operations.

#include <iostream>using namespace Std;int k = 1;typedef struct {float coef;   coefficient int expn;    Index}term;struct lnode{term data; struct Lnode *next;}; typedef struct LNODE* polynomial;//merge similar terms polynomial Mergepolyn (polynomial p) {polynomial p = null,q = Null,r = Null,q =    NULL; if (P = = null| |    P->next = = null) {return null; } else{for (P = p->next;p!=null;p=p->next) for (q = p->next,r = P;q!=null;) {i                    F (p->data.expn = = q->data.expn) {p->data.coef+=q->data.coef;                    Q = q;                    R->next = q->next;                Q = r->next;//? critical Delete q;            } Else R=r->next,q = q->next;    } return P;        }}//1. Create a unary polynomial polynomial Createpolyn (int m) {if (m>=0) {polynomial head = new Lnode;        Head->next = NULL;        Polynomial s,r = head; Forint i=0;i<m;i++) {cout<< "<<i+1<<" the coefficients and indices are: ";            float C;            int e;            cin>>c>>e;            s = new Lnode;            S->next = NULL;            S->data.coef = C;            S-&GT;DATA.EXPN = e;            R->next = s;        r = r->next;        } r->next = NULL;    return head;        } else{cout<< "input m value illegal, create polynomial failed \ n";        k = 0;    return NULL; }}//2. Two unary polynomial add polynomial Addpolyn (const polynomial pa,const polynomial Pb) {if (pa&&pa->next&&pb&        Amp;&pb->next) {Polynomial head = new Lnode;        polynomial s = pa->next,t = Pb->next,r = head;        R->next = NULL;            while (s!=null) {polynomial n = new Lnode;            N->next = NULL;            N->data.coef = s->data.coef;            N-&GT;DATA.EXPN = s->data.expn;            R->next = n;            r = r->next;    s = s->next;    } while (t!=null) {polynomial n = new Lnode;            N->next = NULL;            N->data.coef = t->data.coef;            N-&GT;DATA.EXPN = t->data.expn;            R->next = n;            r = r->next;        t = t->next;        } r->next = NULL;        Head = Mergepolyn (head);    return head; } else if (PA = = null| |    Pa->next = = NULL) && (pb!=null&&pb->next!=null)) return PB; else if (Pb = = null| |    Pb->next = = NULL) && (pa!=null&&pa->next!=null)) return PA; else return NULL;} 3. Two unary polynomial multiplication polynomial Multiplypolyn (const polynomial pa,const polynomial Pb) {if (pa==null| | pa->next==null| | pb==null| |    Pb->next==null) return NULL;        else{polynomial head = new Lnode;        Head->next = NULL;        Polynomial p,q,r,s = head;                for (P = pa->next;p!=null;p=p->next) for (q = pb->next;q!=null;q=q->next) {R = new Lnode;                R->data.coef = P->data.coef * q->data.coef;                R-&GT;DATA.EXPN = p->data.expn + q->data.expn;                R->next = null;//Remember to place the new node pointer field empty s->next = R;            s = s->next;        } s->next = NULL;        Head = Mergepolyn (head);    return head; }}//print polynomial void Printpolyn (polynomial P) {if (p==null| |    P->next==null) {cout<< "no item \ n";        } else{cout<< "y=";        int iszero = 1;        Polynomial p = null,q = NULL;        p = p->next; if (p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn&&        p->data.expn!=1) {cout<<p->data.coef<< "x^" <<p->data.expn;iszero = 0;} else if (p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn==1) {        cout<<p->data.coef<< "X"; iszero = 0;} Elseif (p->data.coef&&p->data.coef!=1&&p->data.coef!=-1&&p->data.expn==0) {cout&        Lt;<p->data.coef;iszero = 0;} else if (p->data.coef==1&&p->data.expn&&p->data.expn!=1) {cout<< "X^" <<p-&gt        ;d Ata.expn;iszero = 0;}        else if (p->data.coef==1&&p->data.expn==1) {cout<< "X"; iszero = 0;}        else if (p->data.coef==1&&p->data.expn==0) {cout<< "1"; iszero = 0;} else if (p->data.coef==-1&&p->data.expn&&p->data.expn!=1) {cout<< "-X^" <<p-&        Gt;data.expn;iszero = 0;}        else if (p->data.coef==-1&&p->data.expn==1) {cout<< "-X"; iszero = 0;}        else if (p->data.coef==-1&&p->data.expn==0) {cout<< "-1"; Iszero = 0;}        else if (p->data.coef==0); else {cout<<p->data.coef<< "x^" < <p->data.expn;iszero = 0;}        if (p->next) q = p->next; while (q) {if (q->data.coef>0&&q->data.coef!=1&&q->data.expn&&q->data.expn            !=1) {cout<< "+" <<q->data.coef<< "x^" <<q->data.expn;iszero = 0;} else if (q->data.coef>0&&q->data.expn==0) {cout<< "+" <<q->data.coef;iszero =            0;} else if (q->data.coef>0&&q->data.coef!=1&&q->data.expn==1) {cout<< "+" <&            lt;q->data.coef<< "X"; iszero = 0;} else if (q->data.coef==1&&q->data.expn&&q->data.expn!=1) {cout<< "+" << "X            ^ "<<q->data.expn;iszero = 0;}            else if (q->data.coef==1&&q->data.expn==1) {cout<< "+" << "X"; iszero = 0;}      else if (q->data.coef==1&&q->data.expn==0)          {cout<< "1"; iszero = 0;} else if (q->data.coef<0&&q->data.coef!=-1&&q->data.expn==1) {cout<<q->d            ata.coef<< "X"; iszero = 0;} else if (q->data.coef==-1&&q->data.expn&&q->data.expn!=1) {cout<< "-X^" <&lt            ; q->data.expn;iszero = 0;}            else if (q->data.coef==-1&&q->data.expn==1) {cout<< "-X"; iszero = 0;}            else if (q->data.coef==-1&&q->data.expn==0) {cout<< "-1"; Iszero = 0;}            else if (q->data.coef==0);            else {cout<<q->data.coef<< "x^" <<q->data.expn;iszero = 0;}        q=q->next;        } if (Iszero) cout<< "0";    cout<<endl;    }}int Main () {polynomial pa = NULL,PB = Null,p = Null,q = Null,r = Null,t = NULL; do{cout<< "1. Creation of two unary polynomial" << Endl;        cout<< "2. Two a new polynomial added to one-tuple polynomial" <<endl;        cout<< "3. Two one-dimensional polynomial multiplication of a new polynomial" <<endl;        cout<< "Please select:";        int n,m;        CIN >> N;                Switch (n) {case 1:cout<< "Please enter the number of items for the first polynomial:";                cin>>m;                P = Createpolyn (m);                PA = Mergepolyn (P);                Printpolyn (PA);                cout<< "Please enter the number of items for the second polynomial:";                cin>>m;                Q = Createpolyn (m);                PB = Mergepolyn (Q);                Printpolyn (PB);            Break                Case 2:r = Addpolyn (PA,PB);                Printpolyn (R);            Break                Case 3:t = Multiplypolyn (PA,PB);                Printpolyn (T);            Break                default:k = 0;                cout<< "Invalid character entered \ n";        Break    }}while (k); return 0;}

Note: You can not arbitrarily modify the value of the PA,PB, two polynomial addition and multiplication should not interfere with each other, in particular, in the polynomial addition can not be used in the two-linked list as a list of the algorithm (will change the PA list).

P.S for each new node of the pointer field to empty, in the same vein, for each delete after the dangling pointer should also be empty, or later when the program used to think that the object is not released, Access will be wrong.

The application of linear table in the operation of one-dimensional polynomial

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.