数据结构与算法——7-2 一元多项式的乘法与加法运算 (20分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。

输入样例:

4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

思路:首先定义好结构,struct node 包括系数,指数,以及next指针,同时使用typedef 将头结点表示和中间结点表示区别开来,用Elementype 表示int 类型,函数分别为创建链表,加法操作,乘法操作以及打印操作
首先创建链表,为头结点分配内存,r为临时指针,从头结点开始往下,head始终指向头指针,依次填入数据
加法操作,比较指数大小,从小到大插入,如果相等,系数相加,当一个表已经插入完成后,另一个表直接将剩下的全部贴到后面,
乘法就是La每一项乘以Lb,然后执行加法操作

#include<stdio.h>
#include<stdlib.h>
 
typedef struct node *ptrNode;
typedef ptrNode LinkList;  //头结点
typedef ptrNode Position;//中间节点
typedef int ElementType;
struct node{
	ElementType Coefficient;//系数
	ElementType Exponent;//指数
	Position next;
};//创建链表

 
int IsEmpty(LinkList L)
{
	return L->next == NULL;
}
 
LinkList creatList(int n)
{
	LinkList head,r,p;
	int coe,exp;
	head = (struct node*)malloc(sizeof(struct node));    //生成新结点
	r = head;
 
	while(n--){
        scanf("%d%d",&coe,&exp);
		p = (struct node*)malloc(sizeof(struct node));
		p->Coefficient = coe;
        p->Exponent = exp;
		r->next = p;
		r = p;
	}
	r->next = NULL;
	return head;
}
 
LinkList add_List(LinkList a, LinkList b)
{
	Position ha, hb;
	LinkList c,r,p;
	int temp;
 
	ha = a->next;
	hb = b->next;
 
	c = (struct node*)malloc(sizeof(struct node));
	r = c;
	while((ha != NULL)&&(hb != NULL)){
		p = (struct node*)malloc(sizeof(struct node));
		if(ha->Exponent < hb->Exponent){
			p->Exponent = hb->Exponent;
			p->Coefficient = hb->Coefficient;
			hb = hb->next;
       r->next = p;//p指针往后挪
       r = p;//r指针往后挪
		}
		else if(ha->Exponent > hb->Exponent){
			p->Exponent = ha->Exponent;
			p->Coefficient = ha->Coefficient;
			ha = ha->next;
            r->next = p;
            r = p;
		}
		else{
            temp = ha->Coefficient + hb->Coefficient;
            if(temp != 0){
                p->Exponent = ha->Exponent;
                p->Coefficient = temp;
                r->next = p;
                r = p;
            }
            hb = hb->next;
            ha = ha->next;
        }
	}
	if(ha == NULL){
		while(hb != NULL){
			p = (struct node*)malloc(sizeof(struct node));
			p->Exponent = hb->Exponent;
			p->Coefficient = hb->Coefficient;
			hb = hb->next;
			r->next = p;
		    r = p;
		}//将b剩下的部分直接贴到p后面
	}
	if(hb == NULL){
		while(ha != NULL){
			p = (struct node*)malloc(sizeof(struct node));
			p->Exponent = ha->Exponent;
			p->Coefficient = ha->Coefficient;
			ha = ha->next;
			r->next = p;
		    r = p;
		}
	}
 
	r->next = NULL;
	return c;
}
 
 
LinkList mul_List(LinkList a, LinkList b)
{
	Position ha, hb;
	LinkList c,tempC,r,p;
	ha = a->next;
    hb = b->next;
    c = creatList(0);
	if(ha == NULL || hb == NULL){
        return c;
	}
 
	while(ha != NULL ){
        tempC = (struct node*)malloc(sizeof(struct node));
        r = tempC;
        hb = b->next;
        while(hb != NULL){
            p = (struct node*)malloc(sizeof(struct node));
            p->Exponent = ha->Exponent + hb->Exponent;
            p->Coefficient = ha->Coefficient*hb->Coefficient;
						hb = hb->next;
            r->next = p;
            r = p;
        }
        r->next = NULL;
        c = add_List(c,tempC);
        ha = ha->next;
	}
	return c;
 
}
void printList(LinkList L)
{
	LinkList hc;
	int flag = 0;
 
	hc = L->next;
	if(hc == NULL)
		printf("0 0");
	while(hc != NULL){
		if(flag)
			printf(" ");
		else
			flag = 1;
		printf("%d %d",hc->Coefficient,hc->Exponent);
		hc = hc->next;
	}
}
 
int main(void)
{
    int n1,n2;
	LinkList L1,L2,L3,L4;
 
    scanf("%d",&n1);
	L1 = creatList(n1);
	scanf("%d",&n2);
	L2 = creatList(n2);
 
	L3 = add_List(L1,L2);
    L4 = mul_List(L1,L2);
	printList(L4);
	printf("\n");
	printList(L3);
 
	return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值