算法思路:两个指针P1和P2分别指向这两个多项式第一个结点,不断循环:
^ P1->expon==P2->expon:系数相加,若结果不为0,则作为结果多项式对应项
的系数。同时,P1和P2都分别指向下一项;
P1->expon>P2->expon:将P1的当前项存入结果多项式,并使P1指向下一项;
> P1->expon<P2->expon:将P2的当前项存入结果多项式,并使P2指向下一项。
代码:
1.构建
typedef struct LinkNode{
int coefficient;
int exponent;
struct LinkNode *next;
}*LinkList,*NodePtr;
ᝰꫛꫀꪝ 17:46:16
初始化
2.初始化
LinkList initLinkList(){
LinkList tempHeader=(LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient=0;
tempHeader->exponent=0;
tempHeader->next=NULL;
return tempHeader;
}
3.打印
void printList(LinkList paraHeader){
NodePtr p=paraHeader->next;
while(p!=NULL){
printf("%d * 10^%d +",p->coefficient,p->exponent);
p=p->next;
}
printf("\r\n");
}
void printNode(NodePtr paraPtr,char paraChar){
if(paraPtr==NULL){
printf("NULL\r\n");
} else{
printf("The element of %c is (%d * 10^%d)\r\n",paraChar,paraPtr->coefficient,paraPtr->exponent);
}
}
4.添加
void appendElement(LinkList paraHeader,int paraCoefficient,int paraExponent){
NodePtr p,q;
q=(NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient=paraCoefficient;
q->exponent=paraExponent;
q->next=NULL;
p=paraHeader;
while(p->next!=NULL){
p=p->next;
}
p->next=q;
}
5.加法函数
void add(Listptr plist1,Listptr plist2){
Nodeptr p,q,r,s,m;
p=plist1->next;q=plist2->next;
s=plist1,m=plist2;
while((p!=NULL)&&(q!=NULL)){
if(p->exponent < q->exponent){
printf("Case 1\n");
s=s->next;
p=p->next;
}
else if(p->exponent > q->exponent){
printf("Case 2\n");
r=(Nodeptr)malloc(sizeof(struct Linknode));
r->coefficient=q->coefficient;
r->exponent=q->exponent;
r->next=p;
s->next=r;
s=r;
m=q;
q=q->next;
free(m);
}
else{
printf("Case 3\n");
p->coefficient=p->coefficient + q->coefficient;
if(p->coefficient==0){
printf("Case 3.1 相等\n");
s->next=p->next;
m=p;p=p->next;
free(m);
}
else{
printf("Case 3.2 不相等\n");
s=s->next;
p=p->next;
}
m=q;
q=q->next;
free(m);
}
}
if(p==NULL){
s->next=q;
}
}
int main()
{
SListNode* P1 = NULL;//存放第一个表达式数据的链表
SListNode* P2 = NULL;//存放第二个表达式数据的链表
SLTDataType coef, expon;
//1.输入两个多项式
int count = 0;
printf("请输入第一个多项式的项数:>");
scanf("%d", &count);
printf("请输入第一个多项式:>");
int i = 0;
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P1, coef, expon);
}
printf("请输入第二个多项式的项数:>");
scanf("%d", &count);
printf("请输入第二个多项式:>");
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P2, coef, expon);
}
//2.分别将两个多项式按指数降序排列
SListNode* SortP1 = InsertSortList(P1);//按指数排序第一个多项式
SListNode* SortP2 = InsertSortList(P2);//按指数排序第二个多项式
printf("第一个多项式按指数降序排列为:>");
SListPrint(SortP1);
printf("第二个多项式按指数降序排列为:>");
SListPrint(SortP2);
//3.将两个多项式相加
SListNode* PolyAddHead = PolyAdd(SortP1, SortP2);//将两个多项式相加
printf("将两个多项式相加之后的结果为:>");
SListPrint(PolyAddHead);
return 0;
}
全部代码
typedef struct LinkNode{
int coefficient;
int exponent;
struct LinkNode *next;
}*LinkList,*NodePtr;
LinkList initLinkList(){
LinkList tempHeader=(LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient=0;
tempHeader->exponent=0;
tempHeader->next=NULL;
return tempHeader;
}
void printList(LinkList paraHeader){
NodePtr p=paraHeader->next;
while(p!=NULL){
printf("%d * 10^%d +",p->coefficient,p->exponent);
p=p->next;
}
printf("\r\n");
}
void printNode(NodePtr paraPtr,char paraChar){
if(paraPtr==NULL){
printf("NULL\r\n");
} else{
printf("The element of %c is (%d * 10^%d)\r\n",paraChar,paraPtr->coefficient,paraPtr->exponent);
}
}
void appendElement(LinkList paraHeader,int paraCoefficient,int paraExponent){
NodePtr p,q;
q=(NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient=paraCoefficient;
q->exponent=paraExponent;
q->next=NULL;
p=paraHeader;
while(p->next!=NULL){
p=p->next;
}
p->next=q;
}
void add(Listptr plist1,Listptr plist2){
Nodeptr p,q,r,s,m;
p=plist1->next;q=plist2->next;
s=plist1,m=plist2;
while((p!=NULL)&&(q!=NULL)){
if(p->exponent < q->exponent){
printf("Case 1\n");
s=s->next;
p=p->next;
}
else if(p->exponent > q->exponent){
printf("Case 2\n");
r=(Nodeptr)malloc(sizeof(struct Linknode));
r->coefficient=q->coefficient;
r->exponent=q->exponent;
r->next=p;
s->next=r;
s=r;
m=q;
q=q->next;
free(m);
}
else{
printf("Case 3\n");
p->coefficient=p->coefficient + q->coefficient;
if(p->coefficient==0){
printf("Case 3.1 相等\n");
s->next=p->next;
m=p;p=p->next;
free(m);
}
else{
printf("Case 3.2 不相等\n");
s=s->next;
p=p->next;
}
m=q;
q=q->next;
free(m);
}
}
if(p==NULL){
s->next=q;
}
}
int main()
{
SListNode* P1 = NULL;//存放第一个表达式数据的链表
SListNode* P2 = NULL;//存放第二个表达式数据的链表
SLTDataType coef, expon;
//1.输入两个多项式
int count = 0;
printf("请输入第一个多项式的项数:>");
scanf("%d", &count);
printf("请输入第一个多项式:>");
int i = 0;
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P1, coef, expon);
}
printf("请输入第二个多项式的项数:>");
scanf("%d", &count);
printf("请输入第二个多项式:>");
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P2, coef, expon);
}
//2.分别将两个多项式按指数降序排列
SListNode* SortP1 = InsertSortList(P1);//按指数排序第一个多项式
SListNode* SortP2 = InsertSortList(P2);//按指数排序第二个多项式
printf("第一个多项式按指数降序排列为:>");
SListPrint(SortP1);
printf("第二个多项式按指数降序排列为:>");
SListPrint(SortP2);
//3.将两个多项式相加
SListNode* PolyAddHead = PolyAdd(SortP1, SortP2);//将两个多项式相加
printf("将两个多项式相加之后的结果为:>");
SListPrint(PolyAddHead);
return 0;
}