数据结构 C++ 单链表实现多项式(不同数值)的表达以及加法

这里由于读题不仔细,导致在实现的时候把多项式表达成了不同数值的系数与指数的操作,在实现的时候也走了弯路,只实现了加法,乘法觉得难以进行。回过头来仔细读题才发现是同一x的多项式操作。对于后者的实现就放到下一篇吧。这一篇就先凑合看看。

#include <iostream>
#include <malloc.h>
using namespace std;

typedef int Elementtype;
struct polynomial {
	Elementtype count;
	Elementtype coenum;
	Elementtype index;
	polynomial *next;
};
typedef polynomial *POLY;


POLY Add(POLY P1, POLY P2) {
	POLY P3;
	P3 = (POLY)malloc(sizeof(polynomial));
	polynomial *s, *r = P3, *p = P1->next, *q = P2->next;
	while (p != NULL && q != NULL) {
		if (p->count == q->count && p->index == q->index) {
			s = (polynomial *)malloc(sizeof(polynomial));
			s->coenum = p->coenum + q->coenum;
			s->count = p->count;
			s->index = p->index;
			r->next = s;
			r = s;
			p = p->next;
			q = q->next;
		} else if (p->index <= q->index) {
			s = (polynomial *)malloc(sizeof(polynomial));
			s->coenum = p->coenum;
			s->count = p->count;
			s->index = p->index;
			r->next = s;
			r = s;
			p = p->next;
		} else {
			s = (polynomial *)malloc(sizeof(polynomial));
			s->coenum = q->coenum;
			s->count = q->count;
			s->index = q->index;
			r->next = s;
			r = s;
			q = q->next;
		}
	}
	while (p != NULL) {
		s = (polynomial *)malloc(sizeof(polynomial));
		s->coenum = p->coenum;
		s->count = p->count;
		s->index = p->index;
		r->next = s;
		r = s;
		p = p->next;
	}
	while (q != NULL) {
		s = (polynomial *)malloc(sizeof(polynomial));
		s->coenum = q->coenum;
		s->count = q->count;
		s->index = q->index;
		r->next = s;
		r = s;
		q = q->next;
	}

	return P3;
}

int main() {
	POLY P1, P2;
	P1 = (POLY)malloc(sizeof(polynomial));
	P2 = (POLY)malloc(sizeof(polynomial));
	polynomial  *s1, *r1 = P1, *print1 = P1;
	polynomial  *s2, *r2 = P2, *print2 = P2;

	//测试数据
	Elementtype a1[5] = {1, 2, 3, 4, 5};
	Elementtype c1[5] = {1, 2, 3, 4, 5};
	Elementtype b1[5] = {1, 3, 6, 3, 4};
	//测试数据
	Elementtype a2[5] = {1, 2, 4, 5, 6};
	Elementtype c2[5] = {1, 2, 3, 4, 5};
	Elementtype b2[5] = {2, 4, 7, 3, 5};

	for (int i = 0; i < 5; i++) {
		s1 = (polynomial *)malloc(sizeof(polynomial));
		s1->count = a1[i];
		s1->coenum = b1[i];
		s1->index = c1[i];
		r1->next = s1;
		r1 = s1;
	}
	r1->next = NULL;

	for (int j = 0; j < 5; j++) {
		s2 = (polynomial *)malloc(sizeof(polynomial));
		s2->count = a2[j];
		s2->coenum = b2[j];
		s2->index = c2[j];
		r2->next = s2;
		r2 = s2;
	}
	r2->next = NULL;

	cout << "The First Polynomial is :" << endl;
	while (print1->next != NULL) {
		cout << print1->next->coenum << "*" << print1->next->count << "^" << print1->next->index ;
		if (print1->next->next != NULL)
			cout << "+";
		print1 = print1->next;
	}
	cout << "\n" << "The Second Polynomial is :" << endl;
	while (print2->next != NULL) {
		cout << print2->next->coenum << "*" << print2->next->count << "^" << print2->next->index ;
		if (print2->next->next != NULL)
			cout << "+";
		print2 = print2->next;
	}

	POLY P3;
	P3 = Add(P1, P2);
	polynomial *print3 = P3;
	cout << "\n" << "The Sum of Polynomial is :" << endl;
	while (print3->next != NULL) {
		cout << print3->next->coenum << "*" << print3->next->count << "^" << print3->next->index ;
		if (print3->next->next != NULL)
			cout << "+";
		print3 = print3->next;
	}

	return 0;
}

主要参考:        《数据结构与算法》(第五版)张岩

                          《数据结构考研复习指导》(王道)

数据结构单链表可以用于实现多项式加法,主要是通过存储多项式的系数和指数(通常用数组表示)。以下是一个简单的Python示例,假设我们有一个节点类`Node`,用于存储一个系数和指数: ```python class Node: def __init__(self, coefficient=0, exponent=0): self.coefficient = coefficient self.exponent = exponent self.next = None class PolynomialList: def __init__(self): self.head = None def add_term(self, coefficient, exponent): new_node = Node(coefficient, exponent) if not self.head: self.head = new_node else: current = self.head while current.next and current.next.exponent > exponent: current = current.next current.next = new_node if current.next is None or current.next.exponent == exponent: current.next.exponent += 1 def add_polynomials(list1, list2): sum_list = PolynomialList() current1 = list1.head current2 = list2.head carry = 0 while current1 or current2: coeff1 = carry if current1: coeff1 += current1.coefficient current1 = current1.next if current2: coeff2 = current2.coefficient current2 = current2.next sum_coeff = coeff1 + coeff2 carry = sum_coeff // (exponent + 1) if sum_coeff >= 10 else 0 sum_list.add_term(sum_coeff % 10, max(current1.exponent, current2.exponent) if current1 and current2 else max(current1.exponent, 0)) if carry > 0: sum_list.add_term(carry, 0) return sum_list # 示例 list1 = PolynomialList() list1.add_term(3, 1) list1.add_term(5, 2) list2 = PolynomialList() list2.add_term(2, 0) list2.add_term(4, 1) result = add_polynomials(list1, list2) ``` 在这个例子中,`add_polynomials`函数会合并两个多项式列表,处理进位并在每个节点添加适当的系数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值