South Mail OJ 1005 polynomial addition

Source: Internet
Author: User

Title Description: Linear table is one of the simplest, most basic and most commonly used data structure, its use is very extensive, for example, a single-linked list with a header node to solve the unary integer coefficient polynomial addition and multiplication operations. now we give two polynomial of one-element integer coefficients, and request the sum of the two solutions.

Topic Link: Click to open the link

Code is borrowed from others, Link: Click to open link

<span style= "FONT-SIZE:18PX;" > #include <iostream>using namespace std;class nodelist;class node//single linked list node {private:int coef;int exp; Node * Link;friend class Nodelist;friend Ostream & operator << (ostream &,const Node &);p Ublic:n Ode (int c, int e): Coef (c), exp (e) {link = 0;} Node (int c,int E, node * next): Coef (c), exp (e) {link = next;} Node * Insert (int c, int e) {link = new Node (c,e,link); return Link;}};o Stream & operator << (Ostream & Out, const Node & val) {if (Val.coef = = 1) {switch (val.exp) {case 0:out &L t;< 1;break;case 1:out << "X"; break;default:out << "x^" << val.exp; break;} return out;} if (Val.coef = =-1) {switch (val.exp) {case 0:out << -1;break;case 1:out << "-X"; Break;default:out << " -x^ "<< Val.exp;break;} return out;} Else{out << Val.coef;switch (val.exp) {case 0:break;case 1:out << "X"; break;default:out<< "x^" < < Val.exp;break;} return out;}} Class nodelist{   Friend Ostream & operator << (ostream &, const nodeList &);    Friend IStream & operator >> (IStream &, NodeList &); Friend NodeList & operator + (nodeList &, NodeList &);p Rivate:node * thelist;public:nodelist (); ~nodelist (); void AddNode (IStream & in), void Output (Ostream & Out) const;void listadd (NodeList & R);}; NodeList:: NodeList () {thelist = new Node (0,-1); thelist->link = thelist;} Nodelist::~nodelist () {Node * p = thelist->link;while (P! = thelist) {Thelist->link = P->link;delete p;p = theList- >link;} Delete thelist;} void NodeList:: AddNode (IStream & in) {Node * q = thelist;int count = 0;int c,e;for (;;) {cin >> C >>e;if (c==0 && e ==-1) {break;} Else{q = Q->insert (c,e);} Count + +;} if (count = = 0) {q = Q->insert (0,0); cout << 0;}} void NodeList:: Output (Ostream & Out) Const{int i=0,j,k=0; Node * m = thelist->link;for (; M!=thelist & m->coef = = 0;m=m->link)   {i++; } m = Thelist->link;for (; m!= thelist; m=m->link) {k++;} m = thelist->link;if (k==1&& M->coef = = 0) {cout << 0 << Endl;return;}        if (k==i) {cout << 0<< Endl;    return;    } for (J =0; j<i;j++) {m=m->link; }out << *m;m=m->link;for (; m!=thelist;m=m->link) {if (M->coef > 0) {cout << "+"; out << *m;} else if (M->coef < 0) {out << *m;}} cout << Endl;}  void Nodelist::listadd (NodeList & R) {Node * q,*q1 = thelist,*p;p = R.thelist->link;q = Q1->link;while (p->exp >= 0) {while (P->exp < q->exp) {Q1=q;q=q->link;} if (p->exp = = q->exp) {Q->coef = P->coef + q->coef;if (P->coef = = 0) {Q1->link = Q->link;delete q;q = Q1->link;}    Else{q1=q;q=q->link;} }elseq1 = Q1->insert (p->coef,p->exp);p =p->link;}} Ostream & operator << (Ostream & Out, const NodeList & x) {x.output (out); return oUT;} IStream & operator >> (IStream & In, NodeList & x) {X.addnode (in); return in;} NodeList & operator + (NodeList & A, NodeList & B) {a.listadd (b); return A;}    int main () {nodeList p,q,q1;    cin>>p,cout<<p;    cin>>q,cout<<q;    Q1=q+p; COUT&LT;&LT;Q1;} </span>

South Mail OJ 1005 polynomial addition

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.