First, the polynomial is a linked list, each of the polynomial is a linked list node, then you can think of two situations:
1) The polynomial has only one or the last item of the polynomial, then the node needs only the coefficients and the exponent two elements, and does not need to point to the next node.
2) One of the polynomial, then the node needs to have coefficients, exponents, and pointers to the next node.
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >class node{private: int coef; int exp; Node * Link;public: node (int c, int e): Coef (c), exp (e) //polynomial does not contain a constructor that points to the next node { link = 0; } node (int c, int E, node * next): Coef (c), exp (e)//contains a constructor that points to the next node { link = next;} } </span></span></span>
The other node should have the function of inserting, so the node class must have the public member function insert () function:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >node * Insert (int c, int e) { link = new Node (c,e,link); return link;} </span></span>
The linked list is made up of nodes, and the linked list has the functions of adding nodes, outputting lists, and linking lists.
<span style= "FONT-SIZE:18PX;" ></pre><pre name= "code" class= "CPP" ><span style= "font-size:18px;" >class nodelist{private: Node * thelist;public: nodeList (); ~nodelist (); void AddNode (IStream & in); void Output (Ostream & Out); void Listadd (NodeList & R);} </span></span>
Third, because the object is a class, for the output operator can not directly output node class, so we want to do an operator overload.
Ostream & operator << (Ostream & Out, const Node & val)
The whole idea is the above three chunks, now a piece of separate analysis.
First block, linked list element node:
<span style= "FONT-SIZE:18PX;" >class Node {private: int coef; coefficient int exp; Index Node * LINK; Pointer to next node public: node (int c, int e): Coef (c), exp (e) //polynomial does not contain a constructor that points to the next node { link = 0; } node (int c, int E, node * next): Coef (c), exp (e)//contains a constructor that points to the next node { link = next; } Node * INSERT (int c, int e)//After the node is inserted into one of the nodes { link = new node (c,e,link); return link; } Friend class NodeList; Links to Operations node friend ostream & operator << (ostream &, const node &); The output of the node is used by the private member of the node}</span>
Second block: Linked list
<span style= "FONT-SIZE:18PX;" >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;} </span>
Third block: operator overloading, output object is node class
<span style= "FONT-SIZE:18PX;" >ostream & operator << (Ostream & Out, const Node & val) {if (Val.coef = = 1) {switch (val.exp) {case 0:o UT << 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;}} </span>
South Mail OJ 1005 polynomial addition (two)