数据结构c语言5:多项式加法

多项式加法
void add(NodePtr paraList1,NodePtr paraList2){
	NodePtr p,q,r,s;
	
	p=paraList1->next;
	q=paraList2->next;
	r=paraList1;
	free(paraList2);
	
	while((p!=NULL)&&(q!=NULL)){
		//case 1
		if(p->exponent<q->exponent){
			r->next=p;
			r=p;
			p=p->next;
		}
		
		//case 2
		else if(p->exponent>q->exponent){
			r->next=q; 
			r=q;
			q=q->next;
		}
		
		//case 3
		else{
			p->coefficient=p->coefficient+q->coefficient;
			
			//case 3.1
			if(p->coefficient==0){
				s=p;
				p=p->next;
				free(s);
			}
			
			//case 3.2
			else{
				r->next=p;
				r=p;
				p=p->next;
			}
			s=q;
			q=q->next;
			free(s);
		}
		if(p==NULL){
			r->next=q;
		}
		else{
			r->next=p;
		}
	}
} 
完整代码
#include<stdio.h>
#include<malloc.h>

//结构体的创建
typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
}*LinkList,*NodePtr;

//初始化
LinkList initLinkList(){
	LinkList tempHeader=(NodePtr)malloc(sizeof(LinkList));
	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 appendElement(LinkList paraHeader,int paracoefficient,int paraexponent){
	NodePtr p,q;
	
	p=paraHeader;
	while(p->next!=NULL){
		p=p->next;
	}
	
	q=(NodePtr)malloc(sizeof(LinkList));
	q->coefficient=paracoefficient;
	q->exponent=paraexponent;
	q->next=NULL;
	
	p->next=q;
} 

//相加
void add(NodePtr paraList1,NodePtr paraList2){
	NodePtr p,q,r,s;
	
	p=paraList1->next;
	q=paraList2->next;
	r=paraList1;
	free(paraList2);
	
	while((p!=NULL)&&(q!=NULL)){
		//case 1
		if(p->exponent<q->exponent){
			r->next=p;
			r=p;
			p=p->next;
		}
		
		//case 2
		else if(p->exponent>q->exponent){
			r->next=q; 
			r=q;
			q=q->next;
		}
		
		//case 3
		else{
			p->coefficient=p->coefficient+q->coefficient;
			
			//case 3.1
			if(p->coefficient==0){
				s=p;
				p=p->next;
				free(s);
			}
			
			//case 3.2
			else{
				r->next=p;
				r=p;
				p=p->next;
			}
			s=q;
			q=q->next;
			free(s);
		}
		if(p==NULL){
			r->next=q;
		}
		else{
			r->next=p;
		}
	}
} 

void test(){
	LinkList tempList1=initLinkList();
	appendElement(tempList1,3,0);
	appendElement(tempList1,3,3);
	appendElement(tempList1,7,5);
	appendElement(tempList1,12,10);
	appendElement(tempList1,5,15);
	printList(tempList1);
	
	LinkList tempList2=initLinkList();
	appendElement(tempList2,5,1);
	appendElement(tempList2,-3,3);
	appendElement(tempList2,2,5);
	appendElement(tempList2,10,9);
	appendElement(tempList2,8,15);
	printList(tempList2);
	
	add(tempList1,tempList2);
	printList(tempList1);
}

int main(){
	test();
}
运行结果
3*10^0+3*10^3+7*10^5+12*10^10+5*10^15+
5*10^1+-3*10^3+2*10^5+10*10^9+8*10^15+
3*10^0+5*10^1+9*10^5+10*10^9+12*10^10+13*10^15+
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值