#include <iostream>
using namespace Std;
struct Node {
Double Coe; Coefficient
int exp; Index
Node *next;
};
void Creatpoly (Node *&head, int n)//Generate a single-linked list with header nodes, generating n nodes in addition to the head node
{
Head = (node *) new Node;
Head->coe = 0;
Head->exp = 0;
Head->next = NULL; Initialize the head node.
cout << "input coefficients and indices for each row:" << Endl;
Node *p = head;
for (int i = 0; i < n; i++) {
P->next = (node *) new Node; Generate new node, end insert Generate list
p = p->next;
CIN >> P->coe >> p->exp;
P->next = NULL;
}
}
void Showpoly (Node *&head)
{
if (Head->next = = NULL)//result is 0 o'clock direct output 0
Putchar (' 0 ');
else {
for (Node *p = head->next; p! = NULL; p = p->next) {
if (P! = Head->next && P->coe >0)//output ' + ' when p is non-first and points to positive coefficients
Putchar (' + '); Previously, only P->coe >0 was judged.
if (P->coe = = 1) {///coefficient 1 or-1 special handling
if (P->exp = = 0)
Putchar (' 1 '); The judging conditions cannot be written together:
}//if (P->coe = = 1 && p->exp = = 0) putchar (' 1 ');
else if (P->coe = =-1)
Putchar ('-');
Else
cout << p->coe;
The index is 0 or 1 o'clock special handling
Switch (P->EXP) {
Case 0:
Break
Case 1:
Putchar (' x ');
Break
Default
P->exp < 0? printf ("x^ (%d)", P->exp): printf ("x^%d", p->exp); Index less than 0 o'clock parentheses
Break
}
}
}
cout << Endl;
}
Char comp (int a, int b)
{
if (a > B)
return ' > ';
if (a < b)
Return ' < ';
return ' = ';
}
void free (Node *&head)
{
Node *q = NULL;
for (Node *p = head; P! = NULL; p = q) {
Q = p->next;
Free (p);
}
}
void addpolynomial (node *&pa, node *&PB)//head pointer passed into two lists
{
Node *ha = PA;
Node *HB = PB;
Node *qa = ha->next; Ha, HB followed by QA, QB in the next position
Node *QB = hb->next; QA, QB points to PA, current comparison element in PB
while (QA && QB)
{
Double sum = 0;
int a = qa->exp;
int B = qb->exp;
Switch (comp (a, b)) {
Case ' < ':
ha = QA;
QA = qa->next; Non-ha = ha->next;
Break
Case ' = ':
sum = Qa->coe + qb->coe;
if (sum! = 0.0) {
Qa->coe = sum;
ha = QA;
}
else {
if (ha->next! = QA)
cout << "Error:ha->next! = QA" << Endl;
Ha->next = ha->next->next; Delete and 0 nodes, ha unchanged, also in QA post-location
Free (QA);
}
if (hb->next! = QB)
cout << "Error:hb->next! = QB" << Endl;
Hb->next = hb->next->next;
Free (QB);
QB = hb->next;
QA = ha->next;
Break
Case ' > ':
Hb->next = hb->next->next; Delete the node the QB points to
Qb->next = ha->next; Before inserting the QB into HA QA
Ha->next = QB;
QB = hb->next; Not QB = Ha->next
Ha = ha->next;
Break
Default
cout << "error!" << Endl;
Break
}
}
if (QB)
Ha->next = QB;
Free (HB);
}
void Main (void)
{
Node *a = NULL;
Node *b = NULL;
int CountA;
int COUNTB;
cout << "Please enter the number of items in a:" << Endl, cin >> CountA;
Creatpoly (A, CountA); Generate a linked list
cout << "Please enter the number of items in B:" << Endl; Generate B-linked list
Cin >> COUNTB;
Creatpoly (B, COUNTB);
cout << "A =";
Showpoly (A);
cout << "B =";
Showpoly (B);
Addpolynomial (A, B); A = a + B
cout << "a+b=";
Showpoly (A); The sum of the outputs
cout << Endl;
Release the node.
Delete A;
}
Data structure addition of unary polynomial for single-linked list of C + +