数据结构4.0链表的应用--多项式加法与乘法

本文介绍了如何使用链表结构实现多项式加法和乘法操作。首先定义了一个链表节点结构,包含系数和指数,然后通过读入多项式数据,使用尾插法构建链表。接着实现了多项式相加和相乘的函数,相加时比较系数,相乘时按照指数大小插入新节点。最后,提供了打印多项式和比较指数的辅助函数。
摘要由CSDN通过智能技术生成

#include <stdio.h>
#include <stdlib.h>
/读入的话才用尾插法,头部保留空结点,插入读入完成后释放头部结点/
/主要结构是:读入,相加(判别相加是否系数为零),相乘(第一项先乘,后续在此基础上按指数大小顺序插入,如果指数大小一样系数相加为零,删除该点/
typedef struct PSTNODE * polynomial;
struct PSTNODE
{
int coef;
int expon;
polynomial pnext;
};
typedef struct PSTNODE node;

polynomial read_poly(void);
polynomial poly_mult(polynomial T1, polynomial T2);
polynomial poly_add(polynomial T1, polynomial T2);
void print_poly(polynomial p);
void attach(int c, int e, polynomial * prear);
int compare(int e1, int e2);
int main(void)
{
polynomial p1, p2, pp, ps;

p1 = read_poly();
p2 = read_poly();
pp = poly_mult(p1, p2);
print_poly(pp);
ps = poly_add(p1, p2);
print_poly(ps);

}

polynomial read_poly(void)
{
polynomial front, rear;
front = (polynomial)malloc(sizeof(node));
rear = front;
rear->pnext = NULL;
int n, i,e, c;
scanf("%d", &n);
while(n–)
{
scanf("%d %d", &c, &e);
if (c != 0)
attach(c, e, &rear);

}
polynomial tmp = front;
front=front->pnext;
free(tmp);
tmp = NULL;
return front;

}

void attach(int c, int e, polynomial * prear)
{
polynomial p = (polynomial) malloc(sizeof(node));
p->coef = c;
p->expon = e;
p->pnext = NULL;
(*prear)->pnext = p;
(*prear) = p;
}

void print_poly(polynomial p)
{
int flag = 0;
polynomial trav = p;
if(!trav)
{
printf(“0 0\n”);
return;
}

while(trav)
{
	if (!flag)
	{
		flag = 1;
	}
	else
	{
		printf(" ");
	}
	printf("%d %d", trav->coef, trav->expon);
	trav=trav->pnext;
}
printf("\n");

return ;

}
polynomial poly_mult(polynomial T1, polynomial T2)
{
polynomial p1, p2, tmp;
p1 = T1;
p2 = T2;
int c, e;
if(!p1 || !p2)
{
return NULL;
}
polynomial front = (polynomial) malloc(sizeof(node));
polynomial rear = front;
rear->pnext = NULL;
while (p2)
{
//p1的第一项与p2的每一项相乘;
attach( p1->coef*p2->coef, p1->expon + p2->expon ,&rear);
p2 = p2->pnext;
}
p1 = p1->pnext;

while (p1)
{
	p2 = T2;
	rear = front;
	while (p2)
	{
		c = p1->coef * p2->coef;
		e = p1->expon + p2->expon;
		while (rear->pnext && rear->pnext ->expon > e)
		{
			rear = rear->pnext;
		}
		if (rear->pnext && rear->pnext->expon == e)
		{
			if (rear->pnext->coef + c == 0)
			{
				tmp = rear->pnext;
				rear->pnext = tmp ->pnext;
				free(tmp);
			}
			else
			{
				rear->pnext->coef = rear->pnext->coef + c;

			}
		}
		else
		{
			//注意这一步是插入, 不是add,注意不要搞混;
			tmp = (polynomial) malloc(sizeof(node));
			tmp->coef = c;
			tmp->expon = e;
			tmp->pnext = rear->pnext;
			rear->pnext = tmp;
			rear = rear->pnext;
		}
		p2 = p2->pnext;
	}
	p1= p1->pnext;
}

tmp = front;
front = front->pnext;
free(tmp);

return front;

}

int compare(int e1, int e2)
{
if(e1>e2)
{
return 0;
}
else if (e1<e2)
{
return -1;
}
else
{
return 1;
}

}
polynomial poly_add(polynomial T1, polynomial T2)
{
int c1, c2;
polynomial front, rear, tmp;
polynomial p1, p2;
p1 = T1;
p2 = T2;
front = (polynomial)malloc(sizeof(node));
rear = front;
int sum;
rear->pnext = NULL;

while (p1 && p2)
{
	
	switch (compare(p1->expon, p2->expon))
	{
	case 1:
		sum=p1->coef + p2->coef;
		if(sum)
		{
			attach(sum, p1->expon, &rear);
		}

		p1=p1->pnext;
		p2=p2->pnext;

		break;
	case 0:
		attach(p1->coef, p1->expon, &rear);
		p1=p1->pnext;
		break;
	case -1:
		attach(p2->coef, p2->expon, &rear);
		p2=p2->pnext;
		break;
	}
}

for (; p1; p1= p1->pnext)
{
	attach(p1->coef, p1->expon, &rear);
}
for (; p2; p2= p2->pnext)
{
	attach(p2->coef, p2->expon, &rear);
}
rear->pnext = NULL; //这部其实没必要;

tmp = front;
front = front->pnext;
free(tmp);
tmp = NULL;


return front;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值