数据结构与算法分析 C 语言描述第二版第三章——链表实现多项式相乘

数据结构与算法分析 C 语言描述第二版第三章——链表实现多项式相乘

思路

  1. 用单链表构建两个多项式,不排序
  2. 一个多项式的每一项分别与另一个多项式所有项相乘,系数相乘,指数相加,得到一个新建的多项式乘积
  3. 多乘积多项式进行从大到小插入排序,排序时遇到指数相等的进行合并节点

程序

#include<stdio.h>
#include<stdlib.h>
struct node;
typedef struct node *ptrtonode;
typedef ptrtonode polynomial;
typedef ptrtonode position;
struct node {
	int coef;
	int expo;
	ptrtonode next;
};
polynomial InitList(void);
void CreatList(polynomial L);
void ShowList(polynomial L);
polynomial MultPolys(polynomial L1, polynomial L2);
void DeleteList(polynomial L);
int main(void) {
	polynomial L1, L2, multpoly;
	L1 = InitList();
	CreatList(L1);
	puts("\nPolynomial1:");
	ShowList(L1);
	L2 = InitList();
	CreatList(L2);
	puts("\nPolynomial2:");
	ShowList(L2);
	puts("\nMultiple two polynomials:");
	multpoly = MultPolys(L1, L2);
	ShowList(multpoly);
	DeleteList(L1);
	DeleteList(L2);
	DeleteList(multpoly);
	return 0;
}
polynomial InitList(void)
{
	polynomial L = (polynomial)malloc(sizeof(struct node));
	if (L == NULL) {
		fprintf(stderr,"Out of space\n");
		exit(EXIT_FAILURE);
	}
	L->next = NULL;
	return L;
}
void InsertTail(int co, int ex, position r)
{
	polynomial tem = (polynomial)malloc(sizeof(struct node));
	tem->coef = co;
	tem->expo = ex;
	tem->next = r->next;
	r->next = tem;
}
void eatline(void)
{
	while (getchar() != '\n')
		continue;
}
void CreatList(polynomial L)
{
	printf("\nPlease enter coefficients and exponents"
	 "(nonumeric values to stop):\n");
	int co, ex;
	position r = L;
	while (scanf("%d %d",&co, &ex) == 2) {
		InsertTail(co, ex, r);
		r = r->next;
	}
	eatline();
}
void ShowList(polynomial L)
{
	position p = L->next;
	while (p) {
		if (p->coef)
			printf("%dX^%d",p->coef,p->expo);
		p = p->next;
		if (p && p->coef > 0)
			putchar('+');
	}
	putchar('\n');
}
/*用插入排序,排序时将同系数项合并
将序列分成已排序和未排序两部分
*/
void MergeNode(position p, position q)
{
	p->coef = p->coef + q->coef;
}
void InsertAfter(position p, position ptemp, position pbtemp)
{
	pbtemp->next = ptemp->next;
	ptemp->next = p->next;
	p->next = ptemp;
}
void SortList(polynomial L)
{
	position p, pbefore, ptemp, pbtemp, tem;
	ptemp = L->next->next;
	pbtemp = L->next;
	while (ptemp) {
		p = L->next;
		pbefore = L;
		while (p != ptemp && ptemp->expo < p->expo) {
				pbefore = p;
				p = p->next;
		}
		if (p == ptemp) {
			pbtemp = ptemp;
			ptemp = ptemp->next;
		}
		else {
	 		if (ptemp->expo == p->expo) {
				MergeNode(p, ptemp);
				tem = ptemp;
				pbtemp->next = ptemp->next;
				ptemp = ptemp->next;
				free(tem);
			}
			else if (ptemp->expo > p->expo) {
				InsertAfter(pbefore, ptemp, pbtemp);
				ptemp = pbtemp->next;
			}
		}
	}
}
int IsEmpty(polynomial L)
{
	return L->next == NULL;
}
polynomial MultPolys(polynomial L1, polynomial L2)
{
	polynomial multpoly = InitList();
	position p1, p2, p = multpoly;
	int co, ex;
	if (IsEmpty(L1) || IsEmpty(L2)) {
		fprintf(stderr,"Empty linked list\n");
		exit(EXIT_FAILURE);
	}
	for (p1 = L1->next; p1 != NULL; p1 = p1->next) 
		for (p2 = L2->next; p2 != NULL; p2 = p2->next) {
			co = p1->coef * p2->coef;
			ex = p1->expo + p2->expo;
			InsertTail(co, ex, p);
			p = p->next;
		}
	SortList(multpoly);
	return multpoly;
}
void DeleteList(polynomial L)
{
	position p, tem;
	p = L->next;
	L->next = NULL;
	while (p) {
		tem = p->next;
		free(p);
		p = tem;
	}
}

测试结果:

Please enter coefficients and exponents(nonumeric values to stop):
3 3
4 4
q

Polynomial1:
3X^3+4X^4

Please enter coefficients and exponents(nonumeric values to stop):
6 2
3 1
q

Polynomial2:
6X^2+3X^1

Multiple two polynomials:
24X^6+30X^5+9X^4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值