多项式的合并-链表实现

描述

键盘输入A,B两多项式,假设
						A = 1x^2 + 2x^3 + 4x^4 + 1x^5
						B = 9x^0 + 4x^1 + 3x^2 + 6x^3
						得到的结果
						C = 9x^0 + 4x + 4x^2 + 8x^3 + 4x^4 + 1x^5
例如:输入时,A表示为   1 2 2 3 4 4 1 5 0 0 ,其中0 0 为存入的终止判断
	 每两个数为一组,分别为A的某项的系数和指数
	 (***A,B都是指数从小到大排序好的多项式)

过程

在这里插入图片描述

1.定义结构体,创建一个函数,用来创建A,B两个多项式,链表储存,x代表系数,y代表指数
typedef struct node
{
   
	int x , y;
	struct node * next;
} * link;
link Create_L() 
{
   
	link H , p , q;
	int a , b;
	H = (node * ) malloc (sizeof(node));
	H -> next = NULL;
	q = H;
	while(1)
	{
   
		cin >> a >> b;
		if(a == 0 && b == 0)
			break;
		link p;
		p = (node *) malloc (sizeof(node));
		p -> next = NULL;
		p -> x = a;
		p -> y = b;
		q -> next = p;
		q = p; 
	}
	return H;
} 
2.函数Create_C()用来完成多项式的合并算法,把开辟好的C和多项式链表A,B传入
void Create_C(link A , link B , link &C)
{
   
	link p , q , k;
	p = A -> next;
	q = B -> next;
	k = C;
	while(q != NULL && p != NULL)
	{
   
		link u = (node *) malloc (
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++链表合并多项式是指将两个或多个多项式链表合并成一个新的多项式链表。每个多项式链表由一系列节点组成,每个节点包含一个系数和指数,表示多项式中的一个项。 下面是C++链表合并多项式的基本步骤: 1. 定义一个节点结构体,包含系数和指数两个成员变量。 2. 定义一个链表结构体,包含一个指向头节点的指针。 3. 创建两个或多个多项式链表,并按照指数从大到小的顺序插入节点。 4. 定义一个新的链表用于存储合并后的多项式。 5. 遍历两个或多个多项式链表,比较当前节点的指数大小,将较小的节点插入新链表,并更新指针。 6. 如果有一个多项式链表遍历完了,将另一个多项式链表剩余的节点直接插入新链表。 7. 返回合并后的多项式链表。 以下是一个示例代码: ```cpp #include <iostream> struct Node { int coefficient; int exponent; Node* next; }; struct LinkedList { Node* head; }; LinkedList mergePolynomials(LinkedList poly1, LinkedList poly2) { LinkedList mergedPoly; Node* p1 = poly1.head; Node* p2 = poly2.head; Node* current = nullptr; while (p1 != nullptr && p2 != nullptr) { Node* newNode = new Node; if (p1->exponent > p2->exponent) { newNode->coefficient = p1->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; } else if (p1->exponent < p2->exponent) { newNode->coefficient = p2->coefficient; newNode->exponent = p2->exponent; p2 = p2->next; } else { newNode->coefficient = p1->coefficient + p2->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; p2 = p2->next; } newNode->next = nullptr; if (current == nullptr) { mergedPoly.head = newNode; current = newNode; } else { current->next = newNode; current = newNode; } } if (p1 != nullptr) { current->next = p1; } if (p2 != nullptr) { current->next = p2; } return mergedPoly; } int main() { // 创建多项式链表1 LinkedList poly1; Node* node1 = new Node{3, 2, nullptr}; Node* node2 = new Node{4, 1, nullptr}; Node* node3 = new Node{2, 0, nullptr}; poly1.head = node1; node1->next = node2; node2->next = node3; // 创建多项式链表2 LinkedList poly2; Node* node4 = new Node{5, 3, nullptr}; Node* node5 = new Node{1, 1, nullptr}; poly2.head = node4; node4->next = node5; // 合并多项式链表 LinkedList mergedPoly = mergePolynomials(poly1, poly2); // 打印合并后的多项式链表 Node* current = mergedPoly.head; while (current != nullptr) { std::cout << current->coefficient << "x^" << current->exponent << " "; current = current->next; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

E鸽子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值