#include <iostream>
using namespace std;#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2typedef int Status;// 定义单链表
typedef struct PNode {float coef; //系数int expn; //指数struct PNode *next;
}PNode, *Polynomial;// 初始化链表
Status InitList_L(Polynomial &P) {P = new PNode;P->next = NULL;return OK;
}// 创建多项式
void CreatePolyn(Polynomial &p, int n) {p = new PNode;p->next = NULL;for (int i = 1; i <= n; ++i) {PNode *s = new PNode;cin >> s->coef >> s->expn;PNode *pre = p;PNode *q = p->next;while (q && p->expn < s->expn) {pre = q;q = q->next;}s->next = q;pre->next = s;}
}//单链表的表长
int ListLength(Polynomial P) {Polynomial p;p = P->next;int i = 0;while (p) {i++;p = p->next;}return i;
}// 遍历打印链表
void TravelList(Polynomial P) {Polynomial p;p = P->next;int len = ListLength(P);for (int i = 0; i < len; i++) {if (p->coef > 0) {cout << p->coef << "x^" << p->expn;if (i < len - 1)cout << " + ";}elsecout << p->coef << "x^" << p->expn ;p = p->next;}cout << endl;
}//多项式的运算
void MergeList(Polynomial Pa, Polynomial Pb) {PNode *p;PNode *p1 = Pa->next;PNode *p2 = Pb->next;PNode *p3 =Pa;int temp = 0;while (p1 && p2){if (p1->expn == p2->expn) {temp = p1->coef + p2->coef;if (temp != 0) {p1->coef = temp;p3 = p1;p1 = p1->next;p = p2;p2 = p2->next;delete p;}else {p = p1;p1 = p1->next;delete p;p = p2;p2 = p2->next;delete p;}}else if (p1->expn < p2->expn) {p3->next = p1;p3 = p1;p1 = p1->next;}else {p3->next = p2;p3 = p2;p2 = p2->next;}}p3->next = p1 ? p1 : p2;delete Pb;
}int main() {Polynomial Pa;Polynomial Pb;CreatePolyn(Pa, 4);CreatePolyn(Pb, 3);MergeList(Pa, Pb);TravelList(Pa);return 0;
}