#include
using namespace std;
typedef struct Poly {
float xishu;
int zhishu;
struct Poly* next;
} LinkList; //创建链表
LinkList *create(int );
void output(LinkList*, LinkList*, LinkList*);
int main()
{
int a, b;
LinkList* h1, *h2 ,*h3;
cout << "请输入第一个多项式的项数:\n";
cin >> a;
h1 = create(a);
cout << "请输入第二个多项式的项数:\n";
cin >> b;
h2 = create(b);
h3 = h1;
output(h1, h2, h3);
delete h1,h2,h3;
return 0;
}
LinkList* create(int n) {
LinkList* head, * node, * end;
head = new LinkList;
end = head;
cout << "请输入多项式的各项系数和指数: \n";
for (int i = 0; i < n; i++) {
node = new LinkList;
cin >> node->xishu >> node->zhishu;
end->next = node;
end = node;
}
end->next = NULL;
return head;
}
void output(LinkList* h1, LinkList* h2, LinkList* h3) {
while (h1->next != NULL || h2->next != NULL) {
if (h1->zhishu == h2->zhishu) {
h3->xishu = h1->xishu + h2->xishu;
h3 = h3->next;
h1 = h1->next;
h2 = h2->next;
}
else if (h1->zhishu > h2->zhishu) {
h3->xishu = h2->xishu;
h3->zhishu = h2->zhishu;
h2 = h2->next;
h3 = h3->next;
}
else if (h1->zhishu < h2->zhishu) {
h3->xishu = h1->xishu;
h3->zhishu = h1->zhishu;
h1 = h1->next;
h3 = h3->next;
}
}
while (h3->next != NULL) {
h3 = h3->next;
cout << h3->xishu << ' ' << h3->zhishu << endl;
}
}
警告 C28182 取消对 NULL 指针的引用。“h1”包含与“h1->next”相同的 NULL 值。
delete h1,h2,h3是错误写法。而且你这h3=h1,怎么能删两遍。另外链表须逐个节点删掉,只删头节点依旧闪存泄漏
output函数完全混乱了。你别忘了h3=h1啊