使用链表进行多项式加法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coulson_Zhao/article/details/87877323

创建一个链表

数据结构选用为单链表,多项式中含有参数和指数,因此链表中包含的元素应该为参数、指数、链表指针,如下所示:

typedef int Elemtype;
struct LkList
{
	int para;
	int index;
	LkList* next;
};
typedef LkList* LIST;
typedef LkList* Position;

链表数据导入

我们首先将数据导入到链表之中,这和链表的创建放在一起。要注意的是,我们要导入所有的指数项,即是说,若一个指数项的参数为0,也要进行导入。

void ini_LkList(LIST L, int para, int index)
{
	Position p, add;
	p = new LkList;
	add = new LkList;
	p->next = L;
	while (p->next != NULL)
	{
		p = p->next;
	}
	p->next = add;
	add->next = NULL;
	add->para = para;
	add->index = index;
}

链表排序

在完成链表的创建后,由于要进行加法,则需要同指数项相加,于是我们需要对链表进行排序。我们参考冒泡排序设计算法,如下所示:

for (int j = 0; j < maxList - 1 - i; j++)
{
	if (three != NULL)
	{
		if (two->index < three->index)
		{
			change(one, two, three);
			display(L);
			one = one->next;
			two = three->next;
			three = two->next;
		}
	}
}

在这里,one代表着待比较两个元素之前的那个元素;two 和 three 代表着待比较元素。
要注意的是最后两行代码,由于进行了换序,因此递推的情况也要进行顺序的交换。

加法

在排序之后,只要将两个链表的对应部分进行相加,我们就可以得到最后相加后的链表了。

完整代码

/*单链表的加法
**HIT Visual Lab
**2019.02.22中午*/

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"

#define maxList 10

using namespace std;

typedef int Elemtype;
struct LkList
{
	int para;
	int index;
	LkList* next;
};
typedef LkList* LIST;
typedef LkList* Position;

void ini_LkList(LIST L, int para, int index);
void display(LIST L);
void change(Position first, Position second, Position third);
void ListSort(LIST L);
LIST adding(LIST L1, LIST L2);

int main()
{
	LIST L1, L2, L;
	L1 = new LkList;
	L2 = new LkList;
	L = new LkList;
	L1->para = 0;
	L1->index = 0;
	L1->next = NULL;
	L2->para = 0;
	L2->index = 0;
	L2->next = NULL;
	//被加数
	for (int i = 0; i < maxList; i++)
	{
		ini_LkList(L1, i - 1, i + 1);
	}
	//加数
	for (int i = 0; i < maxList; i++)
	{
		ini_LkList(L2, -i + 1, i - 1);
	}
	display(L1);
	display(L2);
	ListSort(L1);
	display(L1);
	ListSort(L2);
	display(L2);
	L = adding(L1, L2);
	display(L);
	return 0;
}

//创建链表
void ini_LkList(LIST L, int para, int index)
{
	Position p, add;
	p = new LkList;
	add = new LkList;
	p->next = L;
	while (p->next != NULL)
	{
		p = p->next;
	}
	p->next = add;
	add->next = NULL;
	add->para = para;
	add->index = index;
}

//显示效果
void display(LIST L)
{
	Position p;
	p = L;
	while (p != NULL)
	{
		cout << p->index << " ";
		p = p->next;
	}
	cout << endl;
}

//链表元素交换
void change(Position first, Position second, Position third)
{
	Position p, r;
	p = new LkList;
	r = new LkList;
	first->next = third;
	second->next = third->next;
	third->next = second;
}

//链表排序
void ListSort(LIST L)
{
	Position one, two, three;
	one = new LkList;
	two = new LkList;
	three = new LkList;
	for (int i = 0; i < maxList - 1; i++)
	{
		one = L;
		one->index = L->index;
		one->para = L->para;

		two = L->next;
		two->index = L->next->index;
		two->para = L->next->para;

		three = L->next->next;
		three->index = L->next->next->index;
		three->para = L->next->next->para;

		for (int j = 0; j < maxList - 1 - i; j++)
		{
			if (three != NULL)
			{
				if (two->index < three->index)
				{
					change(one, two, three);
					//display(L);
					one = one->next;
					two = three->next;
					three = two->next;
				}
			}
		}
	}
}

LIST adding(LIST L1, LIST L2)
{
	LIST res;
	res = new LkList;
	res->index = -1;
	res->para = -1;
	res->next = NULL;
	Position p_high, p_low, p;
	if (L1->next->index >= L2->next->index)
	{
		p_high = L1->next;
		p_low = L2->next;
	}
	else
	{
		p_high = L2->next;
		p_low = L1->next;
	}
	while (p_high != NULL)
	{
		if (p_high->index != p_low->index)
		{
			ini_LkList(res, p_high->para, p_high->index);
			p_high = p_high->next;
		}
		else
		{
			ini_LkList(res, p_high->para + p_low->para, p_high->index + p_low->index);
			p_high = p_high->next;
			p_low = p_low->next;
		}
	}
	return res;
}

猜你喜欢

转载自blog.csdn.net/Coulson_Zhao/article/details/87877323