今天学数据结构学到的链表应用于多项式相加,但是书上的代码没看懂,在看了点资料和问ChatGPT以后想到的一个算法,后面有更好的想法在回来更新算法
1.链表相关结构:
//链表结点结构
typedef struct linknode
{
int coef;//系数
int exp;//指数
linknode* next;//指针
}LinkNode;
//链表头结点结构
typedef struct header
{
int length;//链表长度(结点数)
LinkNode* next;//指针
}Header;
2.多项式相加算法:
使用前提:
1.多项式各项按照指数大小降次排序
2.不能出现多个含有相同次幂的项数,例如在一个多项式中不能出现3+5这样,必须为8
,但是这个算法计算结果是没错的,就是输出的时候不太美观,后续会继续完善
//多项式相加
Header* PolyAdd(Header* &A,Header* &B)
{
//创建新链表头结点
Header* C = new Header();
C->length = 0;
C->next = NULL;
LinkNode* pa = A->next;
LinkNode* pb = B->next;
LinkNode* pc = NULL;
int flag2 = 1;
while(pa != NULL && pb != NULL)
{
LinkNode* new_node = new LinkNode();
if(pa->exp > pb->exp)
{
new_node->coef = pa->coef;
new_node->exp = pa->exp;
pa = pa->next;
}
else if(pa->exp == pb->exp)
{
new_node->coef = pa->coef + pb->coef;
new_node->exp = pa->exp;
pa = pa->next;
pb = pb->next;
}
else
{
new_node->coef = pb->coef;
new_node->exp = pb->exp;
pb = pb->next;
}
if(flag2)
{
C->next = new_node;
pc = new_node;
flag2 = 0;
}
else
{
pc->next = new_node;
pc = pc->next;
}
}
//将剩余的没有进行加和的项数结点依次添加到C链表后
//这里由于在上面的循环结束后,A和B链表中必定有一个
//已经到尾了,所以下面对谁先进行排查的顺序无所谓
while(pa != NULL)
{
pc->next = new LinkNode();
pc->next->coef = pa->coef;
pc->next->exp = pa->exp;
pc = pc->next;
pa = pa->next;
}
while(pb != NULL)
{
pc->next = new LinkNode();
pc->next->coef = pb->coef;
pc->next->exp = pb->exp;
pc = pc->next;
pb = pb->next;
}
pc->next = NULL;
return C;
}
3.完整代码:
#include<iostream>
using namespace std;
//链表结点结构
typedef struct linknode
{
int coef;//系数
int exp;//指数
linknode* next;//指针
}LinkNode;
//链表头结点结构
typedef struct header
{
int length;//链表长度(结点数)
LinkNode* next;//指针
}Header;
//创建链表
Header* CreateLinkList()
{
//创建头结点并初始化
Header* header = new Header();
header->length = 0;
header->next = NULL;
//添加链表结点或结束
int choice = 2;
cout << "(1)添加结点 (2)离开:";
cin >> choice;
if(choice == 2)
{
return header;
}
int flag1 = 1;
LinkNode* ptr = NULL;
while(choice == 1)
{
LinkNode* node = new LinkNode();
cout << "系数coef:";
cin >> node->coef;
cout << "指数exp:";
cin >> node->exp;
if(flag1)
{
header->next = node;
ptr = node;
flag1 = 0;
}
else
{
ptr->next = node;
ptr = node;
}
header->length++;
cout << "(1)添加结点 (2)离开:";
cin >> choice;
}
ptr->next = NULL;
return header;
}
//多项式相加
Header* PolyAdd(Header* &A,Header* &B)
{
//创建新链表头结点
Header* C = new Header();
C->length = 0;
C->next = NULL;
LinkNode* pa = A->next;
LinkNode* pb = B->next;
LinkNode* pc = NULL;
int flag2 = 1;
while(pa != NULL && pb != NULL)
{
LinkNode* new_node = new LinkNode();
if(pa->exp > pb->exp)
{
new_node->coef = pa->coef;
new_node->exp = pa->exp;
pa = pa->next;
}
else if(pa->exp == pb->exp)
{
new_node->coef = pa->coef + pb->coef;
new_node->exp = pa->exp;
pa = pa->next;
pb = pb->next;
}
else
{
new_node->coef = pb->coef;
new_node->exp = pb->exp;
pb = pb->next;
}
if(flag2)
{
C->next = new_node;
pc = new_node;
flag2 = 0;
}
else
{
pc->next = new_node;
pc = pc->next;
}
}
//将剩余的没有进行加和的项数结点依次添加到C链表后
//这里由于在上面的循环结束后,A和B链表中必定有一个
//已经到尾了,所以下面对谁先进行排查的顺序无所谓
while(pa != NULL)
{
pc->next = new LinkNode();
pc->next->coef = pa->coef;
pc->next->exp = pa->exp;
pc = pc->next;
pa = pa->next;
}
while(pb != NULL)
{
pc->next = new LinkNode();
pc->next->coef = pb->coef;
pc->next->exp = pb->exp;
pc = pc->next;
pb = pb->next;
}
pc->next = NULL;
return C;
}
//输出链表内容
void Print(Header* &header)
{
LinkNode* ph = header->next;
while(ph != NULL)
{
cout << ph->coef << 'X' << ph->exp;
ph = ph->next;
if(ph != NULL)
{
cout << " + ";
}
}
cout << endl;
system("pause");
system("cls");
}
//清空链表
void FreeLinkList(Header* header)
{
if(header == NULL)
{
cout << "链表不存在或已清空!\n";
return;
}
LinkNode* p = header->next;
LinkNode* temp = NULL;
while(p != NULL)
{
temp = p;
p = p->next;
delete temp;
}
delete header;
header = NULL;
if(header == NULL)
{
cout << "链表已清空!\n";
}
}
//测试函数
void test()
{
cout << "输入多项式A的结点数据:\n";
Header* A = CreateLinkList();
system("pause");
system("cls");
Print(A);
cout << "输入多项式B的结点数据:\n";
Header* B = CreateLinkList();
system("pause");
system("cls");
Print(B);
Header* C = PolyAdd(A,B);
cout << "多项式A+B的结果为:\n";
Print(C);
//销毁链表释放空间
FreeLinkList(A);
FreeLinkList(B);
FreeLinkList(C);
}
int main()
{
test();
return 0;
}