The path to Data Structure Learning-Chapter 2: representation and addition of a polynomial, Data Structure Learning Polynomial

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]

**Preface:**

Finally, the last part of the second chapter of the book has been reached, and the previous content about the linked list has been almost done. Now let's learn the final content of this chapter, how to use a linked list to represent and add a polynomial of a single element. For the performance of a polynomial, it would be very simple to use the subscript method of the sequence table, but this is not ideal for reality, and it is very large to add its maximum power, however, if there are few items, it will waste a lot of space. Therefore, we should use the linked list storage method.

Note:

This article only represents some simple opinions of the bloggers themselves. You are welcome to review and learn to create a good environment together.

The bloggers will try their best to answer some questions, but if any questions are not answered in a timely manner, I hope other enthusiastic people can help solve them. I am very grateful to you.

__Representation and addition of a polynomial__

**1. Storage Structure**

For each unit of polynomials, we need to save the coefficients and exponent of each unit.

Typedef struct // indicates the term. The polynomial term serves as the data element of the LinkList {float coef; // coefficient int expn; // index} term, ElemType;

**2. Specific functions and related implementations**
My. h contains some header files that need to be used

Link. c contains all the functions and settings except the main function in the linked list of the leading node introduced in the previous section.**Yi**

# Include "my. h "// typedef int ElemType; typedef int Status; expression of typedef struct // The polynomial term is used as the data element of the LinkList {float coef; // coefficient int expn; // index} term, ElemType; typedef struct LNode // node type {ElemType data; struct LNode * next;} LNode, * Link, * Position; typedef struct // chain table type {Link head, tail; // point to the head node and the last node int len in the linear chain table; // indicates the number of data elements in the linear chain table} LinkList; typedef LinkList polynomial; # include "link. c "# define DestroyPolyn De StroyList # define PolynLength ListLengthStatus OrderInsertMerge (LinkList * L, ElemType e, int (* compare) (term, term) {// operation result: the compare () function is determined in order () to insert or merge nodes with values of e to the Position q, s; if (LocateElemP (* L, e, & q, compare) of the ascending linked list L )) // This index {q-> data. coef + = e. coef; // change the value of the current node coefficient if (! (Q-> data. coef) // if the coefficient is 0, delete the current node {s = PriorPos (* L, q) in polynomial L; // s is the precursor of the current node if (! S) // q NO precursor s = (* L ). head; DelFirst (L, s, & q); FreeNode (& q);} return OK;} else {if (MakeNode (& s, e )) // generate this index and insert the linked list {InsFirst (L, q, s); return OK;} return ERROR ;}} int cmp (term a, term B) {// Operation Result: returns-1, 0, or + 1 in ascending order if (. expn = B. expn) return 0; if (. expn <B. expn) return-1; return 1;} void CreatPolyn (polynomial * P, int m) {// operation result: Enter the coefficient and exponent of m items, create an ordered chain table P Position q, s; int I; term e; InitList (P); printf ("Enter % d coefficients, exponent:", m); for (I = 1; I <= m; I ++) {scanf ("% f % d", & e. coef, & e. expn); if (! LocateElemP (* P, e, & q, cmp) // This index does not exist in the current linked list. cmp is a real parameter {if (MakeNode (& s, e )) // generate the node and insert the linked list InsFirst (P, q, s) ;}} void PrintPolyn (polynomial P) {// Operation Result: print the output of the unary polynomial P Link q; q = P. head-> next; // q points to the first node printf ("coefficient index \ n"); while (q) {printf ("% f % d \ n ", q-> data. coef, q-> data. expn); q = q-> next;} void AddPolyn (polynomial * Pa, polynomial * Pb) {// Operation Result: polynomial addition: Pa = Pa + Pb, and destroy the Pb Position ha, hb, qa, qb; term a, B; ha = GetHead. (* Pa); hb = GetHead (* Pb); // ha and hb point to the header nodes qa = NextPos (ha) and qb = NextPos (hb) of Pa and Pb respectively ); // qa and qb point to the current node (now the first node) in Pa and Pb while (! ListEmpty (* Pa )&&! ListEmpty (* Pb) & qa) // both Pa and Pb are empty and ha does not point to the End Node (qa! = 0) {a = GetCurElem (qa); B = GetCurElem (qb); // a and B are the current comparison elements of the two tables. switch (cmp (a, B )) {case-1: // The exponent value of the current node in polynomial Pa is small ha = qa; qa = NextPos (qa); // both ha and qa move a node break to the back; case 0: // The exponent values of the two are equal. modify the system value qa-> data of the current Pa node. coef + = qb-> data. coef; if (qa-> data. coef) {ha = qa;} else // if the value is 0, delete the current node {DelFirst (Pa, ha, & qa); FreeNode (& qa);} in polynomial Pa );} delFirst (Pb, hb, & qb); FreeNode (& qb); qb = NextPos (hb); qa = NextPos (ha); break; case 1: // The exponent value of the current node in polynomial Pb is small. DelFirst (Pb, hb, & qb); InsFirst (Pa, ha, qb); qb = NextPos (hb ); ha = ha-> next ;}} if (! ListEmpty (* Pb) {(* Pb ). tail = hb; Append (Pa, qb); // link the remaining node in Pb} DestroyPolyn (Pb); // destroy Pb} void AddPolyn1 (polynomial * Pa, polynomial * Pb) {// operation result: Another Algorithm for polynomial addition is Pa = Pa + Pb, and the Position q; term B; q = GetHead (* Pb) is destroyed ); q = q-> next; while (q) {B = GetCurElem (q); OrderInsertMerge (Pa, B, cmp); q = q-> next ;} destroyPolyn (Pb);} void Opposite (polynomial Pa) {// Operation Result: returns the Inverse Position q for the polynomial coefficient of a single element; q = GetHead (Pa ); // qb points to the header node of Pb q = q-> next; // qb points to the first node of Pb while (q) {q-> data. coef * =-1; q = q-> next;} void SubtractPolyn (polynomial * Pa, polynomial * Pb) {// Operation Result: polynomial subtraction: pa = Pa-Pb, and destroy Pb Opposite (* Pb); AddPolyn (Pa, Pb);} void MultiplyPolyn (polynomial * Pa, polynomial * Pb) {// Operation Result: polynomial multiplication: Pa = Pa * Pb, and the Pb polynomial Pc is destroyed. Position qa, qb; term a, B, c; initList (& Pc); qa = GetHead (* Pa); qa = qa-> next; while (qa) {a = GetCurElem (qa ); qb = GetHead (* Pb); qb = qb-> next; while (qb) {B = GetCurElem (qb); c. coef =. coef * B. coef; c. expn =. expn + B. expn; OrderInsertMerge (& Pc, c, cmp); qb = qb-> next;} qa = qa-> next;} DestroyPolyn (Pb); ClearList (Pa ); (* Pa ). head = Pc. head; (* Pa ). tail = Pc. tail; (* Pa ). len = Pc. len;} int main () {polynomial p, q; int m; printf ("Enter the number of non-zero items of the first polynomial :"); scanf ("% d", & m); CreatPolyn (& p, m); printf ("Enter the number of non-zero items of the Second unary polynomial :"); scanf ("% d", & m); CreatPolyn (& q, m); AddPolyn (& p, & q); printf ("the result of adding two unidimensional polynomials: \ n "); PrintPolyn (p); printf (" Enter the number of non-zero items of the third unary polynomial: "); scanf (" % d ", & m ); creatPolyn (& q, m); AddPolyn1 (& p, & q); printf ("the result of adding two unary polynomials (another method): \ n "); printPolyn (p); printf ("Enter the number of non-zero items of the fourth polynomial:"); scanf ("% d", & m); CreatPolyn (& q, m); SubtractPolyn (& p, & q); printf ("result of two unary polynomials subtraction: \ n"); PrintPolyn (p ); printf ("Enter the number of non-zero items of the Fifth Polynomial:"); scanf ("% d", & m); CreatPolyn (& q, m ); multiplyPolyn (& p, & q); printf ("result of multiplication of two unidimensional polynomials: \ n"); PrintPolyn (p); DestroyPolyn (& p); return 0 ;}

Summary:

It took a lot of time to learn and summarize the knowledge of the second chapter linear table. I don't know if you have basically understood the Principle and Implementation of linear table after learning it? If you still do not understand it, you can perform coding and debugging more. You can improve yourself only by learning how to do it. Next time, we will officially enter the third chapter of stack and queue learning.

Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source