"C + + version of data structure" with the single-linked list of the leading node to realize the unary polynomial (C language version)

Source: Internet
Author: User

The realization of a unary polynomial the structure is as follows:



If only the polynomial "evaluation" and so does not change the polynomial coefficients and exponential operation, using a similar sequential table of the sequential storage structure, otherwise, should adopt the chain storage structure, this article because of a unary polynomial addition, addition, multiplication, so the use of a chain-based storage structure

Polynomail.h

#include <stdio.h> #include <assert.h>struct node{int coef;//coefficient int expn;//exponential};//node structure typedef struct Polynnode{node data;struct polynnode *link;} Polynnode;typedef polynnode* polynmail;void Initpolyn (polynmail &head); void Creatpolyn (Polynmail &head, int n) void Push_back (Polynmail &head, Polynnode *s), void Showpolyn (Polynmail head), void Addpolyn (Polynmail &head, Po Lynmail PA, polynmail pb), void Subpolyn (Polynmail &head, Polynmail PA, polynmail pb), void Mulpolyn (Polynmail &hea D, Polynmail PA, polynmail pb); int lenth (polynmail head); void Destory (Polynmail &head);

Polynomail.cpp

#include "Polynomail.h" void Initpolyn (Polynmail &head) {Polynnode *s = new Polynnode;assert (s! = NULL);s-> Data.coef = 0;S-&GT;DATA.EXPN = -1;s->link = Null;head = s;} void Push_back (Polynmail &head, Polynnode *s) {Polynnode *p = head;while (p->link! = NULL) {p = P->link;} P->link = s;} Create a unary polynomial void Creatpolyn (polynmail &head, int n) {int c;int e;for (int i = 0; i < n; ++i) {printf ("Enter the coefficients and exponents of item%d > ", i + 1); scanf_s ("%d%d ", &c, &e); Polynnode *s = new Polynnode;s->data.coef = C;S-&GT;DATA.EXPN = E;s->link = Null;push_back (head, s);}} Print unary polynomial void Showpolyn (Polynmail head) {Polynnode *s = Head->link;while (s! = NULL) {if (s->data.expn = = 0)//5x^0--- ->5{printf ("%d", S->data.coef);} else if (s->data.expn = = 1)//5x^1---->5x{printf ("%DX", S->data.coef);} else//Normal results Print directly {printf ("%dx^%d", S->data.coef, S-&GT;DATA.EXPN);} The next item is present with a positive coefficient, and you need to print a plus sign (negative numbers will be signed) s = s->link;if (s! = NULL && s->data.coef > 0) {printf ("+");}} printf ("\ n");}//expression PA + PB results saved in head void Addpolyn (Polynmail &head, Polynmail PA, polynmail pb) {Polynnode *sa = pa->link; Polynnode *SB = pb->link; Polynnode *s;while (sa! = null && SB! = null) {s = new Polynnode;s->link = Null;//sa the exponent of the node < The exponent of the SB-node--------- --&GT;//1: Inserting data from the SA node into the results 2: Point to the next PA node if (SA-&GT;DATA.EXPN < SB-&GT;DATA.EXPN) {S->data.coef = sa->data.coef; S-&GT;DATA.EXPN = Sa->data.expn;sa = Sa->link;} Index of the SA node > SB node exponent-----------&GT;//1: Inserting data from SB node into Results 2: point to next PB node else if (Sa->data.expn > Sb->data.expn) { S->data.coef = SB-&GT;DATA.COEF;S-&GT;DATA.EXPN = SB-&GT;DATA.EXPN;SB = Sb->link;} The exponent of the sa node = = the exponent of the SB node//1: Inserts the exponent of both coefficients into the result 2:pa, PB all point to the next node else if (sa->data.expn = = sb->data.expn) {s-> Data.coef = sb->data.coef + sa->data.coef;s->data.expn = SB-&GT;DATA.EXPN;SB = Sb->link;sa = Sa->link;} If the resulting coefficient is 0, you do not need to insert the result, and vice versa needs to be inserted into the result if (S->data.coef! = 0) {push_back (head, s);}} Inserts the remaining nodes of the long expression in PA or PB into the result if (sa! = NULL) {while(sa! = NULL) {s = new Polynnode;s->link = Null;s->data.coef = SA-&GT;DATA.COEF;S-&GT;DATA.EXPN = Sa->data.expn;push_back ( Head, s); sa = Sa->link;}} if (SB! = null) {while (SB! = null) {s = new Polynnode;s->link = Null;s->data.coef = sb->data.coef;s->data.expn = Sb->data.expn;push_back (head, s); sb = Sb->link;}}} The result of expression PA-PB is saved in head void Subpolyn (Polynmail &head, Polynmail PA, polynmail pb) {Polynnode *sa = pa->link; Polynnode *SB = pb->link; Polynnode *s;while (sa! = null && SB! = NULL) {s = new Polynnode;s->link = NULL;//SA-SB in the SA and a node that does not exist in SB, subtract positive I F (SA-&GT;DATA.EXPN < SB-&GT;DATA.EXPN) {S->data.coef = SA-&GT;DATA.COEF;S-&GT;DATA.EXPN = Sa->data.expn;sa = Sa->link;} A node that exists in the SA-SB SB and does not exist in the SA, minus the else if (sa->data.expn > sb->data.expn) {s->data.coef =-(SB-&GT;DATA.COEF); S-&GT;DATA.EXPN = SB-&GT;DATA.EXPN;SB = Sb->link;} SA-SB the nodes that both exist, the result = The result of the subtraction of the other if (sa->data.expn = = sb->data.expn) {S->data.coef =SB-&GT;DATA.COEF-SA-&GT;DATA.COEF;S-&GT;DATA.EXPN = SB-&GT;DATA.EXPN;SB = Sb->link;sa = Sa->link;} if (S->data.coef! = 0) {push_back (head, s);}} if (sa! = null) {while (sa! = null) {s = new Polynnode;s->link = Null;s->data.coef = sa->data.coef;//sa-sb SA exists in A node that does not exist in SB and is subtracted from positive s->data.expn = Sa->data.expn;push_back (head, s); sa = Sa->link;}} if (SB! = null) {while (SB! = null) {s = new Polynnode;s->link = Null;s->data.coef =-(SB-&GT;DATA.COEF);//SA-SB SB A node that exists and does not exist in the SA, minus s->data.expn = Sb->data.expn;push_back (head, s); sb = Sb->link;}} int Lenth (Polynmail head) {int count = 0; Polynnode *s = Head->link;while (s! = NULL) {count++;s = S->link;} return count;} Expression PA * PB results are saved in head void Mulpolyn (Polynmail &head, Polynmail PA, polynmail pb) {Polynnode *SB = Pb->link;while (s b! = NULL) Each node in the//SB is multiplied by all nodes in the sa {polynnode *sa = pa->link;//because every node of SB is multiplied by all nodes in the SA, so each node of SB is while (sa! = NULL)// When multiplying, the SA always starts from the beginning {//creates the multiplied node polynnode *p = new polynnode;p->data.coef = (SA-&GT;DATA.COEF) * (SB-&GT;DATA.COEF);//coefficients multiplied p->data.expn = (SA-&GT;DATA.EXPN) + (SB-&GT;DATA.EXPN );//index addition P->link = null;//to find out if there is a node in the existing node that is the same as the node you just created (exponent) Polynnode *s = Head->link;while (s! = NULL && &GT;DATA.EXPN)! = (P-&GT;DATA.EXPN)) {s = s->link;} There is no node with the same exponent, the direct tail plug can be if (s = = NULL) {push_back (Head, p);} There are nodes with the same exponent, the coefficient can be else{s->data.coef + = P->data.coef;} (SB's current node) to be multiplied by the next node in the SA to prepare sa = sa->link;} Prepare SB = Sb->link for the next node in SB by multiplying all nodes of SA; void Destory (Polynmail &head) {Polynnode *s = Head->link;while (s! = NULL) {Head->link = S->link;delete S;s = h Ead->link;} Delete Head;head = NULL;}

main.cpp

#include "Polynomail.h" #include <cstdlib>int main () {Polynmail pa; Polynmail PB; Polynmail Pc;initpolyn (PA); Initpolyn (Pb); Initpolyn (PC); int select = 1;int n;while (select) {printf ("***************** \ n ");p rintf (" * [1] Create polynomial PA [2] Create polynomial PB *\n ");p rintf (" * [3] Print polynomial PA [4] Print polynomial PB *\n ");p rint           F ("* [5]addpolyn [6]subpolyn *\n");p rintf ("* [7]mulpolyn [8] Print polynomial PC *\n");p rintf ("* [0] Exit system [0] Exit System *\n ");p rintf (" *****************************************\n "); scanf_s ("%d ", &select); switch (select) {CA Se 1:printf ("Please enter the number of entries in the polynomial:>"); scanf_s ("%d", &n); Creatpolyn (PA, n); Break;case 2:printf ("Please enter the number of items in the polynomial:>"); scanf_s ("%d", &n); Creatpolyn (Pb, N); Break;case 3:printf ("PA ="); Showpolyn (PA); Break;case 4:printf ("PB ="); Showpolyn (PB); break;case 5:addpolyn (PC, PA, Pb) break;case 6:subpolyn (PC, PA, Pb); break;case 7:mulpolyn (PC, PA, PB); Break;case 8:printf ("PC ="); Showpolyn (PC); break;default:break;}} Destory (PA);d estory (pb);d estory (PC);System ("pause"); return 0;} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C + + version of data structure" with the single-linked list of the leading node to realize the unary polynomial (C language version)

Related Article

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.