数据结构之链表-多项式相加

本文介绍了一种基于排序思想的多项式相加算法,通过查找合适位置进行底数和幂次的匹配相加,实现了多项式的高效合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多项式相加

主要是利用了类似于排序的思想,从第一个开始找,直到找到合适的位置,如果幂是一样的,则直接相加,否则就插入一个新的节点。


// ConsoleApplication2.cpp : Defines the entry point for the console application.
//author:jwfy
//time:2014-1-13 pm 4:19
 
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef struct Node
{
	int x;      // 底数
	int num;    //  幂
	Node *next;
}Node,*Lnode;

void Add(Node& node,int x,int num)
{
	Lnode p=node.next,q=new Node,r=&node;
	q->next=NULL;
	q->num=num;
	q->x=x;
	if(!p)   //  表示一个元素都没有
	{
		q->next=node.next;
		node.next=q;
		return ;
	}
    while(p)
	{
		if(p->num>=num)
			break;
		r=p;
		p=p->next;
	}
    if(p)   //  
	{
		if(p->num==num)
			p->x+=x;
		else
		{
			q->next=r->next;
			r->next=q;
		}
		return ;
	}
	q->next=r->next;
	r->next=q;
}
void Prin(Node node)
{
	Lnode p=node.next;
	for(;p;p=p->next)
	{
		cout<<p->x<<"x^"<<p->num;
		if(p->next)
			cout<<"+";
	}
	cout<<endl;
}

int main()
{
	Node head;
	head.next=NULL;
	Add(head,2,4);
	Add(head,3,2);
	Add(head,2,8);
	Add(head,1,4);
	Prin(head);
	return 0;
}


对于两个多项式 $A(x)$ 和 $B(x)$,我们可以将它们表示为以下形式: $$ A(x) = a_n x^n + a_{n-1} x^{n-1} + \cdots + a_1 x + a_0 $$ $$ B(x) = b_m x^m + b_{m-1} x^{m-1} + \cdots + b_1 x + b_0 $$ 其中 $n$ 和 $m$ 分别表示两个多项式的最高次幂,$a_i$ 和 $b_j$ 分别表示 $x^i$ 和 $x^j$ 的系数。 我们可以将两个多项式相加,得到一个新的多项式 $C(x)$: $$ C(x) = A(x) + B(x) = c_k x^k + c_{k-1} x^{k-1} + \cdots + c_1 x + c_0 $$ 其中 $k = \max(n, m)$,$c_i = a_i + b_i$。 为了实现多项式相加,我们可以用链表来存储多项式链表的每个节点表示一个单项式,包含系数和次数两个属性。具体地,我们可以设计一个结构体来表示单项式: ```c++ struct Node { int coef; // 系数 int exp; // 次数 Node* next; // 指向下一个节点的指针 }; ``` 然后,我们可以用一个链表来表示一个多项式链表的每个节点存储一个单项式。链表的头指针指向第一个节点,尾指针指向最后一个节点。具体地,我们可以设计一个结构体来表示多项式: ```c++ struct Poly { Node* head; // 指向第一个节点的指针 Node* tail; // 指向最后一个节点的指针 }; ``` 接下来,我们可以实现一个函数来创建一个多项式。这个函数会从标准输入中读取多项式的每个单项式,然后将其添加到链表中。具体地,我们可以设计一个函数如下: ```c++ Poly create_poly() { Poly poly; poly.head = poly.tail = nullptr; // 初始化链表为空 int coef, exp; while (cin >> coef >> exp) { Node* node = new Node{coef, exp, nullptr}; if (poly.tail == nullptr) { poly.head = poly.tail = node; } else { poly.tail->next = node; poly.tail = node; } } return poly; } ``` 最后,我们可以实现一个函数来计算两个多项式的和。这个函数会遍历两个链表,将每个单项式相加,然后将其添加到结果链表中。具体地,我们可以设计一个函数如下: ```c++ Poly add_poly(const Poly& poly1, const Poly& poly2) { Poly res; res.head = res.tail = nullptr; // 初始化链表为空 Node *p1 = poly1.head, *p2 = poly2.head; while (p1 != nullptr && p2 != nullptr) { if (p1->exp > p2->exp) { Node* node = new Node{p1->coef, p1->exp, nullptr}; if (res.tail == nullptr) { res.head = res.tail = node; } else { res.tail->next = node; res.tail = node; } p1 = p1->next; } else if (p1->exp < p2->exp) { Node* node = new Node{p2->coef, p2->exp, nullptr}; if (res.tail == nullptr) { res.head = res.tail = node; } else { res.tail->next = node; res.tail = node; } p2 = p2->next; } else { Node* node = new Node{p1->coef + p2->coef, p1->exp, nullptr}; if (res.tail == nullptr) { res.head = res.tail = node; } else { res.tail->next = node; res.tail = node; } p1 = p1->next; p2 = p2->next; } } while (p1 != nullptr) { Node* node = new Node{p1->coef, p1->exp, nullptr}; if (res.tail == nullptr) { res.head = res.tail = node; } else { res.tail->next = node; res.tail = node; } p1 = p1->next; } while (p2 != nullptr) { Node* node = new Node{p2->coef, p2->exp, nullptr}; if (res.tail == nullptr) { res.head = res.tail = node; } else { res.tail->next = node; res.tail = node; } p2 = p2->next; } return res; } ``` 这样,我们就实现了用链表来表示多项式,并且计算两个多项式的和。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值