数据结构——多项式加法

算法思路:两个指针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;
}


 

  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值