散列表保存多项式链表相乘的结果

      多项式用链表保存,设多项式的最高次为M和N,则申请一个大小为M+1+N的散列表,其中M+1+N不必是素数,用结果多项式的阶次对M+1+N取余作为散列函数,可以把多项式第i阶次项的系数保存在散列表第i项上,方便后续重建多项式的链表。

// sparse_polynomial.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

struct ListNode;                     //声明存多项式的链表
typedef struct ListNode *List;
struct HashTbl;                     //用于存结果的散列表,大小为M+N
typedef struct HashTbl *HashTable;

struct ListNode
{
	double coefficient;
	int degree;
	List Next;
	int maxDegree;
};

List InitializeList(char Tag, int n)                               //链表初始化函数
{
	List L = (List)malloc(sizeof(ListNode));
	L->Next = NULL;
	cout << "请输入" << Tag << "最高阶次:";
	cin >> L->maxDegree;
	cout << endl;
	for (int i = 0; i < n; i++) {
		List p= (List)malloc(sizeof(ListNode));
		cout << "请输入"<<Tag<< "目标项阶次:";
		cin >> p->degree;
		cout << "请输入" << Tag << "该项系数:";
		cin >> p->coefficient;
		cout << endl;
		p->Next = L->Next;
		L->Next = p;
	}
	return L;
}

void printList(List L)                           //多项式打印函数
{
	while (L->Next != NULL) {
		if (L->Next->coefficient != 1) 
			cout << L->Next->coefficient << "x^" << L->Next->degree;
		else
			cout << "x^" << L->Next->degree;
		if (L->Next->Next != NULL)cout << "+";
		L = L->Next;
	}
	cout << endl;
}


struct HashTbl
{
	int TableSize;
	double *result;
};


HashTable InitializeTable(int TableSize)                         //初始化散列表
{
	HashTable H=(HashTable)malloc(sizeof(HashTbl)); 
	if (H == NULL)
		cout << "out of space";
	H->TableSize = TableSize+1;
	H->result = (double *)malloc(sizeof(double)*H->TableSize);       //申请一片地址空间
	if (H->result == NULL)
		cout << "out of space";
	for (int i = 0; i < H->TableSize; i++)
		H->result[i] = 0;
	return H;
}

int Hash(int key, int tableSize)
{
	return key%tableSize;
}

HashTable multiplication(const List A, const List B)        //用散列表保存乘法的结果
{
	int M = A->maxDegree;
	int N = B->maxDegree;
	HashTable H = InitializeTable(M+N);                      //申请一个M+N+1大小的散列表
	List nodeA = A->Next;
	List nodeB = B->Next;
	int resultDegree;                                    
	double resultCoefficient;
	while (nodeA) {
		nodeB = B->Next;
		while (nodeB)
		{
			resultDegree = nodeA->degree + nodeB->degree;
			resultCoefficient = nodeA->coefficient*nodeB->coefficient;
			H->result[Hash(resultDegree, H->TableSize)] += resultCoefficient;  //散列表第几个槽代表第几阶次;
			nodeB = nodeB->Next;
		}
		nodeA = nodeA->Next;
	}
	return H;
}

List makeResultList(HashTable H)                                    //检测散列表中不为0的项存在结果链表上
{
	List ResultList = (List)malloc(sizeof(ListNode));
	ResultList->Next = NULL;
	int maxDegree = H->TableSize-1;
	ResultList->maxDegree = maxDegree;
	for (int i = 0; i<=maxDegree; i++)
		if (H->result[i] != 0) {
			List p = (List)malloc(sizeof(ListNode));
			p->degree = i;
			p->coefficient = H->result[i];
			p->Next = ResultList->Next;
			ResultList->Next = p;
		}
	return ResultList;
}

int main()
{
	int m, n;
	cout << "请输入第一个多项式A的项数:";
	cin >> m;
	List A = InitializeList('A',m);
	cout << "请输入第二个多项式B的项数:";
	cin >> n;
	cout << endl;
	List B = InitializeList('B', n);
	printList(A);
	printList(B);
	HashTable H = multiplication(A, B);
	List Result = makeResultList(H);
	printList(Result);
	while (1);
    return 0;
}

  

posted @ 2017-03-29 15:54  lineaar  阅读(178)  评论(0编辑  收藏  举报