求助:基于链表实现多项式加法与减法的代码问题
链表实现多项式加减的解决方案
嘿,我懂你现在卡在多项式加减的实现上了——别慌,咱们先把你没写完的链表节点定义补全,再一步步拆解核心逻辑,很快就能搞定!
首先,先补全你的链表节点结构(修正了命名让逻辑更清晰):
#include <iostream> #include <cmath> #include <cstddef> using namespace std; typedef float polyType; struct polyInfo { polyType coefficient; // 把你原来的`number`改成更直观的系数命名 int power; polyInfo* next; // 补上链表的后继指针,这是链表结构的关键 };
核心思路:多项式加减的本质
多项式加减的核心是按指数匹配节点处理:
- 指数相同的项:系数相加(减法等价于加负系数),结果不为0才保留
- 指数不同的项:直接保留到结果链表中
- 处理完其中一个链表后,把另一个链表剩余的节点直接追加到结果里
1. 实现多项式加法函数
咱们用哨兵节点简化空链表的边界处理(不用反复判断头节点是否为空),代码如下:
// 创建单个多项式节点的工具函数 polyInfo* createNode(polyType coeff, int pow) { polyInfo* newNode = new polyInfo; newNode->coefficient = coeff; newNode->power = pow; newNode->next = nullptr; return newNode; } // 多项式加法:poly1 + poly2 polyInfo* addPolynomials(polyInfo* poly1, polyInfo* poly2) { // 哨兵节点,避免处理空链表的繁琐判断 polyInfo* resultHead = createNode(0, 0); polyInfo* current = resultHead; while (poly1 != nullptr && poly2 != nullptr) { if (poly1->power == poly2->power) { // 指数相同,系数相加 polyType sumCoeff = poly1->coefficient + poly2->coefficient; // 系数不为0才加入结果(避免出现0x^n这类无效项) if (sumCoeff != 0) { current->next = createNode(sumCoeff, poly1->power); current = current->next; } poly1 = poly1->next; poly2 = poly2->next; } else if (poly1->power > poly2->power) { // 第一个多项式的项指数更大,直接复制到结果 current->next = createNode(poly1->coefficient, poly1->power); current = current->next; poly1 = poly1->next; } else { // 第二个多项式的项指数更大,直接复制到结果 current->next = createNode(poly2->coefficient, poly2->power); current = current->next; poly2 = poly2->next; } } // 处理第一个链表剩余的节点 while (poly1 != nullptr) { current->next = createNode(poly1->coefficient, poly1->power); current = current->next; poly1 = poly1->next; } // 处理第二个链表剩余的节点 while (poly2 != nullptr) { current->next = createNode(poly2->coefficient, poly2->power); current = current->next; poly2 = poly2->next; } // 跳过哨兵节点,返回真正的结果头 polyInfo* temp = resultHead; resultHead = resultHead->next; delete temp; // 释放哨兵节点的内存,避免泄漏 return resultHead; }
2. 实现多项式减法函数
减法不用重复写逻辑——只需要把第二个多项式的所有系数取反,再调用加法函数即可:
// 辅助函数:复制多项式并将所有系数取反 polyInfo* negatePolynomial(polyInfo* poly) { if (poly == nullptr) return nullptr; polyInfo* negHead = createNode(-poly->coefficient, poly->power); polyInfo* current = negHead; poly = poly->next; while (poly != nullptr) { current->next = createNode(-poly->coefficient, poly->power); current = current->next; poly = poly->next; } return negHead; } // 多项式减法:poly1 - poly2 polyInfo* subtractPolynomials(polyInfo* poly1, polyInfo* poly2) { polyInfo* negPoly2 = negatePolynomial(poly2); polyInfo* result = addPolynomials(poly1, negPoly2); // 记得释放取反后的临时多项式,避免内存泄漏(可以自己写一个deletePolynomial函数) // deletePolynomial(negPoly2); return result; }
3. 额外:打印多项式(方便测试验证)
为了快速验证结果,咱们写一个打印函数:
void printPolynomial(polyInfo* poly) { if (poly == nullptr) { cout << "0" << endl; return; } while (poly != nullptr) { // 第一个项前面不加+号 if (poly != nullptr && poly->coefficient > 0) { cout << "+"; } cout << poly->coefficient << "x^" << poly->power; poly = poly->next; } cout << endl; }
注意事项
- 内存管理:用完链表后一定要写一个
deletePolynomial函数释放所有节点的内存,避免内存泄漏 - 确保你的多项式链表是按指数降序排列的,否则上面的加减逻辑会出错;如果不是,需要先给链表排序
- 可以根据需求调整系数的类型(比如把
polyType改成int)
内容的提问来源于stack exchange,提问作者Parker Mathis
相关产品推荐
相关产品推荐

