Data Structure practice-linked list: polynomial summation, data structure Polynomial
This article focuses on the basic series of online courses on data structures (2): practical projects of linear tables.
[Project-polynomial summation]
A single-chain table is used to store a single-dimensional polynomial and add two polynomials.
Tip:
1. Data Structure for storing Polynomials
The general formula of polynomial is Pn (x) = anxn + an −1xn −1 +... + a1x + a0 . There are n + 1 polynomial in total. Intuitively, you can define an array to store the n + 1 coefficients. Take Polynomials P (x) = −3.4x10 − 9.6x8 + 7.2x2 + x For example, an array storing this polynomial is as follows:
We can see that this solution is suitable for processing certain polynomials. However, there is a waste of space when processing Polynomials with a high number of times but few items, and there will be a lot of idle zeros.
You can use the single-chain table structure defined below to store polynomials: each node in the linked list is one of the polynomials. The data field of the knots includes two parts: the index and the coefficient, and each item in the polynomial is connected by the pointer field.
Typedef struct pnode // defines the node type of a single-chain table, stores one of the polynomials, and the linked list forms a polynomial {
Double coef; // coefficient, floating point number
Int exp; // exponential, positive integer *
Struct pnode * next; // pointer to the next item
} PolyNode;
Shows the list used to represent polynomials. When a polynomial linked list is created, nodes are arranged in an order from large to small in an exponential order.
2. Implementation of Polynomial addition in the linked list Storage Structure
Under the linked list storage structure, the implementation of Polynomial addition is based on the single-chain table storage structure defined above, and the algorithm for realizing polynomial addition is discussed.
The rule for adding two polynomials is to add the coefficients to the items with the same index. Let's set the head pointers of the linked list of two polynomials to be added as head1 (first polynomial) and head2 (second polynomial) respectively, and save the sum of the two to the linked list head1. You only need to first use the first node of the head1 and head2 linked lists as the current node (pointing to p1 and p2 respectively) to start detection. In the process of the traversal chain table, the situation is as follows:
(1) If the exponent values of the current nodes in the two polynomials are the same, their coefficients are added, the results are saved to the p1 node, and the p2 node is deleted. If the coefficient after addition is not 0, p1 points to the next node of the first polynomial and prepares for subsequent work. Otherwise, if the coefficient is not saved as 0, the current p1 node is deleted.
(2) When the exponent values of the corresponding nodes in two polynomials are not equal, if p1 points to a node with a large index, p1 simply points to the next node; when p2 points to a large node, it is necessary to insert the p2 node before p1, and then p2 points back to the next node in the second polynomial to continue processing.
(3) The detection process ends until any of the linked lists. If p1 is not empty, the remainder of the first polynomial is already in the linked list and is not processed. If p2 is not empty, you only need to link p2 to the end of the first Polynomial After addition.
The above discussion assumes that the polynomial linked list has been sorted by exponent from large to small. Before addition, a variety of measures are taken to ensure this premise is true.
[[Reference]
# Include <stdio. h> # include <malloc. h> # define MAX 20 // maximum number of polynomials typedef struct // defines the array type for storing polynomials {double coef; // coefficient int exp; // exponent} PolyArray; typedef struct pnode // defines the node type of a single-chain table and stores one of the polynomials. The linked list forms a polynomial {double coef; // coefficient int exp; // returns the struct pnode * next;} PolyNode; void DispPoly (PolyNode * L) // output polynomial {bool first = true; // true indicates that the first PolyNode * p = L-> next; while (p! = NULL) {if (first) first = false; else if (p-> coef> 0) printf ("+"); if (p-> exp = 0) printf ("% g", p-> coef); else if (p-> exp = 1) printf ("% gx", p-> coef ); else printf ("% gx ^ % d", p-> coef, p-> exp); p = p-> next;} printf ("\ n ");} void DestroyList (PolyNode * & L) // destroy the single-chain table {PolyNode * p = L, * q = p-> next; while (q! = NULL) {free (p); p = q; q = p-> next;} free (p);} void CreateListR (PolyNode * & L, PolyArray a [], int n) // create the table {PolyNode * s, * r; int I; L = (PolyNode *) malloc (sizeof (PolyNode )); // create a header node L-> next = NULL; r = L; // r always points to the terminal node. At the beginning, it points to the header node for (I = 0; I <n; I ++) {s = (PolyNode *) malloc (sizeof (PolyNode); // create a new node s-> coef = a [I]. coef; s-> exp = a [I]. exp; r-> next = s; // insert * s into * r and r = s;} r-> next = NULL; // set the next field of the terminal node to NULL} void Sort (PolyNo De * & head) // sort by exp domain in descending order {PolyNode * p = head-> next, * q, * r; if (p! = NULL) // if the original single linked list contains one or more data nodes {r = p-> next; // r stores the pointer p-> next = NULL for * p node successor nodes; // constructs an ordered table p = r with only one data node. while (p! = NULL) {r = p-> next; // r stores * p node successor node pointer q = head; while (q-> next! = NULL & q-> next-> exp> p-> exp) q = q-> next; // find the forward node of * p in the ordered table * q p-> next = q-> next; // insert * p to * q and then q-> next = p; p = r ;}} void Add (PolyNode * ha, PolyNode * hb, PolyNode * & hc) // calculate the sum of two Ordered Sets and complete addition {PolyNode * pa = ha-> next, * pb = hb-> next, * s, * tc; double c; hc = (PolyNode *) malloc (sizeof (PolyNode); // create a header node tc = hc; while (pa! = NULL & pb! = NULL) {if (pa-> exp> pb-> exp) {s = (PolyNode *) malloc (sizeof (PolyNode )); // copy node s-> exp = pa-> exp; s-> coef = pa-> coef; tc-> next = s; tc = s; pa = pa-> next;} else if (pa-> exp <pb-> exp) {s = (PolyNode *) malloc (sizeof (PolyNode )); // copy node s-> exp = pb-> exp; s-> coef = pb-> coef; tc-> next = s; tc = s; pb = pb-> next;} else // pa-> exp = pb-> exp {c = pa-> coef + pb-> coef; if (c! = 0) // create a node {s = (PolyNode *) malloc (sizeof (PolyNode) when the sum of coefficients is not 0 )); // copy node s-> exp = pa-> exp; s-> coef = c; tc-> next = s; tc = s;} pa = pa-> next; pb = pb-> next ;}} if (pb! = NULL) pa = pb; // copy the remaining node while (pa! = NULL) {s = (PolyNode *) malloc (sizeof (PolyNode); // copy node s-> exp = pa-> exp; s-> coef = pa-> coef; tc-> next = s; tc = s; pa = pa-> next;} tc-> next = NULL ;} int main () {PolyNode * ha, * hb, * hc; PolyArray a [] = {1.2, 0}, {2.5, 1}, {3.2, 3 }, {-2.5, 5 }}; PolyArray B [] = {-1.2, 0}, {2.5, 1}, {3.2, 3}, {2.5, 5 }, {5.4, 10 }}; CreateListR (ha, a, 4); CreateListR (hb, B, 5); printf ("original polynomial A:"); DispPoly (ha ); printf ("original polynomial B:"); DispPoly (hb); Sort (ha); Sort (hb); printf ("ordered polynomial A:"); DispPoly (ha ); printf ("ordered polynomial B:"); DispPoly (hb); Add (ha, hb, hc); printf ("polynomial addition:"); DispPoly (hc ); destroyList (ha); DestroyList (hb); DestroyList (hc); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.