一元多项式
链表实现
#include<map>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define null NULL
#define polymal (poly*)malloc(sizeof(poly))
using namespace std;
struct poly{
pair<int, int> data;
poly* next;
};
void read(poly* a) {
poly* poi = a;
int n, xs, cs;
cin >> n;
for(int i = 0; i < n; i++) {
poly* nex = polymal;
cin >> cs >> xs;
nex->data = make_pair(xs, cs);
poi->next = nex;
poi = poi->next;
}
poi->next = null;
}
void add(poly* a, poly* b, poly* ans) {
poly* apo = a->next, *bpo = b->next, *cpo = ans;
while(apo && bpo) {
poly* sum = polymal;
if(apo->data.first < bpo->data.first) {
sum->data = apo->data;
apo = apo->next;
} else if(apo->data.first > bpo->data.first) {
sum->data = bpo->data;
bpo = bpo->next;
} else {
sum->data = make_pair(apo->data.first, apo->data.second+bpo->data.second);
apo = apo->next, bpo = bpo->next;
if(!sum->data.second) {
free(sum);
continue;
}
}
cpo->next = sum;
cpo = cpo->next;
}
while(apo) {
poly* sum = polymal;
sum->data = apo->data;
cpo->next = sum;
cpo = cpo->next;
apo = apo->next;
}
while(bpo) {
poly* sum = polymal;
sum->data = bpo->data;
cpo->next = sum;
cpo = cpo->