# Include <iostream> # include <malloc. h> using namespace std; # define MAX 20 // maximum number of polynomials typedef struct // defines the array type for storing polynomials {float coef; // coefficient int exp; // index} PolyArray [MAX]; typedef struct pNode // defines the node type of the single-chain table {float coef; int exp; struct pNode * next;} PolyNode; void DispPoly (PolyNode * L) // output polynomial {PolyNode * p = L-> next; while (p! = NULL) {printf ("% gX ^ % d", p-> coef, p-> exp); p = p-> next ;} printf ("\ n");} void CreateListR (PolyNode * & L, PolyArray a, int n) // create a table using the tail plug method {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, r = s;} r-> next = NULL; // set the next field of the terminal node to NULL} void Sort (PolyNod E * & head) // sort by exp domain in descending order {PolyNode * p = head-> next, * q, * r; if (p! = NULL) // if one or more data nodes exist in the original single linked list {r = p-> next; // r stores the pointer p-> next = NULL for * p node's successor node; // constructs an ordered table p = r; while (p! = NULL) {r = p-> next; // r stores the * p node's successor node pointer q = head; while (q-> next! = NULL & q-> next-> exp> p-> exp) q = q-> next; // find the precursor node * qp-> next = q-> next of * p inserted in the ordered table; // insert * p to * q and then q-> next = p; p = r ;}} void Add (PolyNode * ha, PolyNode * hb, PolyNode * & hc) // calculate the sum of {PolyNode * pa = ha-> next, * pb = hb-> next, * s, * tc; float 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; 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); s-> exp = pa-> exp; s-> coef = pa-> coef; tc-> next = s; tc = s; pa = pa-> next;} tc-> next = NULL;} void 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 ("sorted polynomial A:"); DispPoly (ha); printf ("sorted polynomial B: "); DispPoly (hb); Add (ha, hb, hc); printf (" polynomial addition: "); DispPoly (hc );}