#include<iostream>
using namespace std;
typedef struct LinkNode{
int coefficient;//系数
int exponent;//指数
struct LinkNode* next;//指针域
}*LinkList,*NodePtr;
//定义数据结构
LinkList initLinkList(){
LinkList p=(LinkList)malloc(sizeof(LinkNode));
p->coefficient=0;
p->exponent=0;
p->next=nullptr;
return p;
}
//初始化
void printList(LinkList paraHeader){
LinkList p=paraHeader->next;
while(p!=nullptr){
printf("%d * x^%d + ", p->coefficient, p->exponent);
p=p->next;
}
printf("\r\n");
}
//打印链表元素
void printNode(LinkList paraPtr,char paraChar){
if (paraPtr==nullptr) {
printf("nullptr\r\n");
}
else{
printf("The element of %c is (%d * x^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
}
}
//打印元素
void appendElement(LinkList paraHeader,int paracoefficient,int paraexponent){
LinkList p,q;
q=(LinkList)malloc(sizeof(LinkNode));
q->coefficient=paracoefficient;
q->exponent=paraexponent;
q->next=nullptr;
p=paraHeader;
while(p->next!=nullptr){
p=p->next;
}
p->next=q;
}
//插入新的链表元素
void add(LinkList paraList1,LinkList paraList2){
LinkList p,q,s,r;
p=paraList1->next;
printNode(p,'p');
q=paraList2->next;
printNode(q,'q');
r=paraList1;
printNode(r,'r');
free(paraList2);
while((p!=nullptr)&&(q!=nullptr)){
if(p->exponent<q->exponent){
printf("case 1\r\n");
r->next=p;
r=p;
printNode(r,'r');
p=p->next;
printNode(p,'p');
}
else if(p->exponent>q->exponent){
printf("case 2\r\n");
r->next=q;
r=q;
printNode(r,'r');
q=q->next;
printNode(q,'q');
}
else{
printf("case 3\r\n");
p->coefficient=p->coefficient+q->coefficient;
printf("The coefficient is: %d.\r\n",p->coefficient);
if(p->coefficient==0){
printf("case 3.1\r\n");
s=p;
p=p->next;
printNode(p,'p');
free(s);
}
else{
printf("case 3.2\r\n");
r=p;
printNode(r,'r');
p=p->next;
printNode(p,'p');
}
s=q;
q=q->next;
free(s);
}
}
printf("End of while.\r\n");
if(p==nullptr){
r->next=q;
}
else{
r->next=p;
}
printf("Addition ends.\r\n");
}
//多项式加法
void additionTest1(){
LinkList tempList1=initLinkList();
appendElement(tempList1,7,0);
appendElement(tempList1,3,1);
appendElement(tempList1,9,8);
appendElement(tempList1,5,17);
printList(tempList1);
LinkList tempList2=initLinkList();
appendElement(tempList2,8,1);
appendElement(tempList2,22,7);
appendElement(tempList2,-9,8);
printList(tempList2);
add(tempList1,tempList2);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
void additionTest2(){
LinkList tempList1=initLinkList();
appendElement(tempList1,7,0);
appendElement(tempList1,3,1);
appendElement(tempList1,9,8);
appendElement(tempList1,5,17);
printList(tempList1);
LinkList tempList2=initLinkList();
appendElement(tempList2,8,1);
appendElement(tempList2,22,7);
appendElement(tempList2,-9,10);
printList(tempList2);
add(tempList1,tempList2);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}
int main(){
additionTest1();
additionTest2();
printf("Finish.\r\n");
return 0;
}
首先,代码中使用了链表来表示多项式,每个节点包含了系数和指数,并通过指针连接起来,形成一个链表结构。这种数据结构在处理多项式加法问题时非常适用,因为它能够动态地添加和删除节点,而无需提前确定多项式的长度。
其次,代码中定义了一系列函数来对链表进行初始化、打印、插入元素和进行多项式加法等操作。这些函数的设计使得代码结构清晰,功能模块化,易于理解和维护。
在多项式加法函数 add
中,通过比较指数的大小来决定节点的插入位置,从而实现了多项式的加法。在相同指数的情况下,将两个节点的系数相加,并根据结果进行处理。通过逐步遍历两个多项式的节点,实现了多项式的相加。
在测试函数中,通过创建两个多项式,分别进行了多项式加法的测试,并打印了最终的结果。通过测试,验证了代码的正确性和可靠性。
通过阅读和理解这段代码,我对链表、多项式加法以及C++中的指针操作有了更深入的了解,这将对我的编程能力和算法思维有所提升。同时,通过实践和调试,我也掌握了一些调试技巧和代码优化的方法,使得自己的编程水平得到了提高。