栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构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->exponentexponent){
			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=p;
				p=p->next;
			}
			s=q;
			q=q->next;
			free(s);
		}
		if(p==NULL){
			r->next=q;
		}
		else{
			r->next=p;
		}
	}
} 
完整代码
#include
#include

//结构体的创建
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("rn");
} 

//尾插
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->exponentexponent){
			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=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+
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号