山东大学数据结构实验报告矩阵和散列表_第1页
山东大学数据结构实验报告矩阵和散列表_第2页
山东大学数据结构实验报告矩阵和散列表_第3页
山东大学数据结构实验报告矩阵和散列表_第4页
山东大学数据结构实验报告矩阵和散列表_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、山东大学计算机科学与技术学院数据结构课程实验报告 学号:姓名:徐大鹏班级:实验题目:实验四_矩阵和散列表实验学时:2实验日期:2015.11.24实验目的:掌握特殊矩阵和稀疏矩阵。掌握散列表及其应用。硬件环境: 略软件环境:Ubuntu Kylin 15.04 64-bitLinux GCC 4.9.2Java SE Runtime Environment (build 1.8.0_60-b27)Eclipse IDE for C/C+ Developers Mars.1 Release (4.5.1)实验内容与设计:1. 实验内容(题目内容,输入要求,输出要求)(1)创建

2、三对角矩阵类,采用按列映射方式,提供store和retrieve 方法。(2)创建下三角矩阵类,采用按列映射方式,提供store和retrieve 方法。(3)创建稀疏矩阵类,采用行主顺序把稀疏矩阵映射到一维数组中,实现稀疏矩阵的转置和两个稀疏矩阵的加法操作。(4)使用散列表设计实现一个字典,假设关键字为整数且D为961,在字典中插入随机产生的500个不同的整数,实现字典的建立和搜索操作。分别使用线性开型寻址和链表散列解决溢出。2.数据结构与算法描述(整体思路描述,所需要的数据结构与算法)对问题一,从数学上推导得出三对角方阵列主映射的函数关系式i = 2c + r - 3其中i为元素在数组e中

3、的下标, c为列数, r为行数,c1且r1。以此关系式为TridiagonalMatrix类编写了Store和Retrieve函数,并扩展编写了Input函数和Output函数。对问题二,从数学上推导得出下三角方阵列主映射的函数关系式i = n × (c - 1) - 1 + r + c × (1 - c) / 2其中i为元素在数组e中的下标,n为方阵的大小,c为列数, r为行数,c1且r1。以此关系式为LowerTriangularMatrix类编写了Store和Retrieve函数,并扩展编写了Input函数和Output函数。对问题三,仿课本所述,定义Term类作为S

4、parseMatrix类的友元类,包含行、列、值三个要素的成员变量,用Term类的数组实现稀疏矩阵的行主映射存储。查找行为的实现方式是,找到位于目标元素前一行的最后一个元素,再从这个元素开始向下搜索,直到找到和目标元素同一行但是列数小于目标元素的元素ak-1,然后决定下一步的行为插入一个新项Term作为ak并将已有元素向后移位,还是修改已存在的项ak。以此原理编写了Store和Retrieve函数,并扩展编写了Input函数和Output函数。对问题四,仿照课本例子编写了有序链表类SortedChain、开放寻址的散列表类HashTable、基于有序链表链接的散列表类ChainHashTabl

5、e,并对这三个类分别扩展编写了Output函数。3. 测试结果(测试输入,测试输出)问题一:问题二:上图显示了输入不符合下三角方阵约束时,抛出异常并退出程序。上图是正常运行的结果。问题三:普通的输入和输出操作如下:矩阵相加:矩阵转置:问题四:以上图的数据为例。从346就应该在链表链接的散列表上看到开放寻址解决冲突的例子。返回开放寻址的输出段,可以看到符合预期的结果:4.实现源代码(程序风格清晰易理解,有充分的注释)/* * TridiagonalMatrix.h * * Created on: Nov 22, 2015 * Author: xudp */#ifndef TRIDIAGONALM

6、ATRIX_H_#define TRIDIAGONALMATRIX_H_#include <iostream>using namespace std;template<class T>class TridiagonalMatrix public:/ 1、创建三对角矩阵类,采用按列映射方式,提供 store 和 retrieve 方法。TridiagonalMatrix(int size = 10);TridiagonalMatrix();/ row>0, column>0TridiagonalMatrix<T>& Store(int ro

7、w, int column, const T& value);T Retrieve(int row, int column);void Input(istream& in, ostream& out);void Output(ostream& out) const;friend ostream& operator<< (ostream& out, const TridiagonalMatrix<T>& matrix)matrix.Output(out);return out;friend istream&

8、operator>> (istream& in, TridiagonalMatrix<T>& matrix)matrix.Input(in, cout);return in;private:T* e;/ Store all elementsint size;template class TridiagonalMatrix<double>#endif /* TRIDIAGONALMATRIX_H_ */* * TridiagonalMatrix.cpp * * Created on: Nov 22, 2015 * Author: xudp */

9、#include "TridiagonalMatrix.h"#include "Exceptions.h"template<class T>TridiagonalMatrix<T>:TridiagonalMatrix(int size) if (size < 1)throw InvalidParameterValue();e = new T3 * size - 2;this->size = size;template<class T>TridiagonalMatrix<T>:Tridiagona

10、lMatrix() delete e;template<class T>TridiagonalMatrix<T>& TridiagonalMatrix<T>:Store(int row, int column,const T& value) if (row < 1 | row > size | column < 1 | column > size| (row - column) > 1 | (row - column) < -1)throw InvalidParameterValue();e2 * colu

11、mn + row - 3 = value;return *this;template<class T>T TridiagonalMatrix<T>:Retrieve(int row, int column) if (row < 1 | row > size | column < 1 | column > size| (row - column) > 1 | (row - column) < -1)throw InvalidParameterValue();return e2 * column + row - 3;template<

12、;class T>void TridiagonalMatrix<T>:Input(istream& in, ostream& out) out << "请按行主顺序依次输入元素," << endl;out << "元素个数必须恰好是" << (3 * size - 2) << "个: "<< endl;for (int i = 0; i < size; i+) for (int j = i - 1; j <= i +

13、1; j+) if (i = 0 && j = -1)continue;if (i = size - 1 && j = size)continue;T element;in >> element;Store(i + 1, j + 1, element);out << "操作成功完成. " << endl;template<class T>void TridiagonalMatrix<T>:Output(ostream& out) const for (int i = 0; i

14、 < size; i+) for (int j = 0; j < size; j+)if (i - j) > 1 | (i - j) < -1)out << "0t"else out << e2 * j + i << "t"out << endl;/* * SparseMatrix.h * * Created on: Oct 20, 2015 * Author: xudp */#ifndef SPARSEMATRIX_H_#define SPARSEMATRIX_H_#include

15、 <iostream>using namespace std;template<class T> class SparseMatrix;template<class T>class Term friend SparseMatrix<T> ;private:int row, col;T value;template<class T>class SparseMatrix public:/* 3、创建稀疏矩阵类,采用行主顺序把稀疏矩阵映射到一维数组中,实现稀 * 疏矩阵的转置和两个稀疏矩阵的加法操作。 */SparseMatrix(int

16、maxTerms = 10);SparseMatrix(const SparseMatrix<T>& spm);SparseMatrix() delete a;void Transpose(SparseMatrix<T> &b) const;void Add(const SparseMatrix<T> &b, SparseMatrix<T> &c) const;/* * Write the store and retrieve functions for a sparse matrix stored in * ro

17、w-major order in a one-dimensional array. */SparseMatrix<T>& Store(const T& x, int i, int j);T Retrieve(int i, int j) const;void Input(istream& in, ostream& out);void Output(ostream& out) const;friend ostream& operator<<(ostream& out, const SparseMatrix<T&g

18、t;& matrix) matrix.Output(out);return out;friend istream& operator>>(istream& in, SparseMatrix<T>& matrix) matrix.Input(in, cout);return in;int GetMaxSize()return MaxTerms;private:void Append(const Term<T>& t);int rows, cols; / matrix dimensionsint terms; / curr

19、ent number of nonzero termsTerm<T> * a; / term arrayint MaxTerms; / size of array a;template class SparseMatrix<double> ;#endif /* SPARSEMATRIX_H_ */* * SparseMatrix.cpp * * Created on: Oct 20, 2015 * Author: xudp */#include "SparseMatrix.h"#include "Exceptions.h"temp

20、late<class T>SparseMatrix<T>:SparseMatrix(int maxTerms) / Sparse matrix constructor.if (maxTerms < 1)throw BadInitializers();MaxTerms = maxTerms;a = new Term<T> MaxTerms;terms = cols = rows = 0;template<class T>SparseMatrix<T>& SparseMatrix<T>:Store(const T

21、& theVal, int theRow,int theCol) if (theRow < 1 | theCol < 1 | theRow > rows | theCol > cols)throw OutOfBounds();int cursor = -1;do cursor+;if (cursor = terms) Term<T> t;t.row = theRow;t.col = theCol;t.value = theVal;Append(t);return *this; while (acursor.row < theRow| (acur

22、sor.row = theRow && acursor.col < theCol);if (acursor.row = theRow && acursor.col = theCol) / (theRow,theCol) is already existent.acursor.value = theVal;else if (terms >= MaxTerms)throw NoMem();for (int k = terms - 1; k >= cursor; k-) ak + 1 = ak;acursor.row = theRow;acursor

23、.col = theCol;acursor.value = theVal;terms+;return *this;template<class T>T SparseMatrix<T>:Retrieve(int theRow, int theCol) const if (theRow < 1 | theCol < 1 | theRow > rows | theCol > cols)throw OutOfBounds();int cursor = 0;while (acursor.row < theRow| (acursor.row = the

24、Row && acursor.col < theCol) cursor+;if (cursor = terms) / not in the sparse matrixreturn 0;if (acursor.row = theRow && acursor.col = theCol) / (theRow,theCol) is already existent.return acursor.value;elsereturn 0;template<class T>void SparseMatrix<T>:Output(ostream&am

25、p; out) const out << "最大容许行数:" << rows << " 最大容许列数:" << cols << " 非零元素数:" << terms<< endl;/ put terms, one per linefor (int i = 0; i < terms; i+)out << "a(" << ai.row << ',' << ai.col

26、 << ") = " << ai.value<< endl;template<class T>void SparseMatrix<T>:Input(istream& in, ostream& out) out << "分别输入行数最大值,列数最大值,以及本次输入的元素组数:"int theTerms;in >> rows >> cols >> theTerms;if (theTerms > MaxTerms)throw NoM

27、em();/ input termsint theRow, theCol;T theVal;for (int i = 0; i < theTerms; i+) out << "依次输入第" << (i + 1) << "项的所在的行、列,以及它对应的数值"in >> theRow >> theCol >> theVal;Store(theVal, theRow, theCol);template<class T>void SparseMatrix<T>:

28、Transpose(SparseMatrix<T> &b) const / Return transpose of *this in b./ make sure b has enough spaceif (terms > b.MaxTerms)throw NoMem();/ set transpose characteristicsb.cols = rows;b.rows = cols;b.terms = terms;/ initialize to compute transposeint *ColSize, *RowNext;ColSize = new intcol

29、s + 1;RowNext = new introws + 1;/ find number of entries in each column of *thisfor (int i = 1; i <= cols; i+) / initializeColSizei = 0;for (int i = 0; i < terms; i+)ColSizeai.col+;/ find the starting point of each row of bRowNext1 = 0;for (int i = 2; i <= cols; i+)RowNexti = RowNexti - 1 +

30、 ColSizei - 1;/ perform the transpose copying from *this to bfor (int i = 0; i < terms; i+) int j = RowNextai.col+; / position in bb.aj.row = ai.col;b.aj.col = ai.row;b.aj.value = ai.value;template<class T>void SparseMatrix<T>:Add(const SparseMatrix<T> &b, SparseMatrix<T&

31、gt; &c) const / Compute c = (*this) + b./ 矩阵规模匹配if (rows != b.rows | cols != b.cols)throw SizeMismatch();c.cols = cols;c.rows = rows;/ 重新初始化稀疏矩阵cdelete c.a;int newMaxTerms = b.terms + terms;c.MaxTerms = (newMaxTerms > c.MaxTerms) ? newMaxTerms : c.MaxTerms;c.a = new Term<T> c.MaxTerms;/

32、 Not the best way.bool* hasMatched_b = new boolb.terms;for (int i = 0; i < b.terms; i+)hasMatched_bi = false;int curIndex = 0;for (int i = 0; i < terms; i+) bool isMatched = false;for (int j = 0; j < b.terms; j+) if (ai.row = b.aj.row && ai.col = b.aj.col) isMatched = true;hasMatche

33、d_bj = true;c.acurIndex.row = ai.row;c.acurIndex.col = ai.col;c.acurIndex.value = ai.value + b.aj .value;break;if (!isMatched) c.acurIndex.row = ai.row;c.acurIndex.col = ai.col;c.acurIndex.value = ai.value;curIndex+;for (int i = 0; i < b.terms; i+) if (!hasMatched_bi) c.acurIndex.row = b.ai.row;c

34、.acurIndex.col = b.ai.col;c.acurIndex.value = b.ai.value;curIndex+;/ When writing this function by myself, I forgot the following:c.terms = curIndex;delete hasMatched_b;template<class T>void SparseMatrix<T>:Append(const Term<T>& t) / Append a nonzero term t to *thisif (terms &g

35、t;= MaxTerms)throw NoMem();aterms = t;terms+;/* * SortedChain.h * * Created on: Nov 9, 2015 * Author: xudp */#ifndef SORTEDCHAIN_H_#define SORTEDCHAIN_H_#include <iostream>using namespace std;/* template note: E denotes the data type of the chain elements, and K * that of the keys on which the

36、 chain is sorted. */template<class E, class K> class SortedChain;template<class E, class K>class SortedChainNode friend class SortedChain<E, K> ;private:E data;SortedChainNode<E, K> *link;template<class E, class K>class SortedChain public:SortedChain() first = 0;SortedC

37、hain();bool IsEmpty() const return first = 0;int Length() const;bool Search(const K& k, E& e) const;SortedChain<E, K>& Delete(const K& k, E& e);SortedChain<E, K>& Insert(const E& e);SortedChain<E, K>& DistinctInsert(const E& e);void Output(ostrea

38、m& out) const;friend ostream& operator<< (ostream& out, const SortedChain<E,K>& sc)sc.Output(out);return out;private:SortedChainNode<E, K> *first;template class SortedChainNode<long, long> ;template class SortedChain<long, long> ;#endif /* SORTEDCHAIN_H_

39、 */* * SortedChain.cpp * * Created on: Nov 9, 2015 * Author: xudp */#include "SortedChain.h"#include "Exceptions.h"template<class E, class K>SortedChain<E, K>:SortedChain() SortedChainNode<E, K>* p = first;SortedChainNode<E, K>* tmp;while (p) tmp = p;p = p

40、->link;delete tmp;template<class E, class K>int SortedChain<E, K>:Length() const SortedChainNode<E, K> *p = first;int ret = 0;while (p) ret+;p = p->link;return ret;template<class E, class K>bool SortedChain<E, K>:Search(const K& k, E& e) const / Put elemen

41、t that matches k in e./ Return false if no match.SortedChainNode<E, K> *p = first;/ search for match with kfor (; p && p->data < k; p = p->link);/ verify matchif (p && p->data = k) / yes, found matche = p->data;return true;return false;template<class E, class

42、K>SortedChain<E, K>& SortedChain<E, K>:Delete(const K& k, E& e) / Delete element that matches k./ Put deleted element in e./ Throw BadInput exception if no match.SortedChainNode<E, K> *p = first, *tp = 0; / trail p/ search for match with kfor (; p && p->da

43、ta < k; tp = p, p = p->link);/ verify matchif (p && p->data = k) / found a matche = p->data; / save data/ remove p from chainif (tp)tp->link = p->link;elsefirst = p->link; / p is first node.delete p;return *this;throw BadInput(); / no matchtemplate<class E, class K>

44、;SortedChain<E, K>& SortedChain<E, K>:Insert(const E& e) SortedChainNode<E, K>* p;if (!first | first->data >= e) p = new SortedChainNode<E, K>();p->data = e;p->link = first;first = p; else p = first;while (p->link) if (p->link->data < e) p = p-

45、>link; else SortedChainNode<E, K>* ncn = new SortedChainNode<E, K>();ncn->data = e;ncn->link = p->link;p->link = ncn;break;if (!p->link) p->link = new SortedChainNode<E, K>();p = p->link;p->link = 0;p->data = e;return *this;template<class E, class K

46、>SortedChain<E, K>& SortedChain<E, K>:DistinctInsert(const E& e) / Insert e only if no element with same key/ currently in list./ Throw BadInput exception if duplicate.SortedChainNode<E, K> *p = first, *tp = 0; / trail p/ move tp so that e can be inserted after tpfor (;

47、p && p->data < e; tp = p, p = p->link);/ check if duplicateif (p && p->data = e)throw BadInput();/ not duplicate, set up node for eSortedChainNode<E, K> *q = new SortedChainNode<E, K>q->data = e;/ insert node just after tpq->link = p;if (tp)tp->link =

48、 q;elsefirst = q;return *this;template<class E, class K>void SortedChain<E, K>:Output(ostream& out) constSortedChainNode<E, K> *p = first;if (!first)out << "(Empty chain.)"elsewhile(p)out << p->data << "t"p = p->link;out << endl

49、;/* * LowerTriangularMatrix.h * * Created on: Nov 22, 2015 * Author: xudp */#ifndef LOWERTRIANGULARMATRIX_H_#define LOWERTRIANGULARMATRIX_H_#include <iostream>using namespace std;template<class T>class LowerTriangularMatrix public:/ 2、创建下三角矩阵类,采用按列映射方式,提供 store 和 retrieve 方法。LowerTriangu

50、larMatrix(int size = 10);LowerTriangularMatrix();/ row>0, column>0LowerTriangularMatrix<T>& Store(int row, int column, const T& value);T Retrieve(int row, int column);void Input(istream& in, ostream& out);void Output(ostream& out) const;friend ostream& operator<

51、;<(ostream& out,const LowerTriangularMatrix<T>& matrix) matrix.Output(out);return out;friend istream& operator>>(istream& in, LowerTriangularMatrix<T>& matrix) matrix.Input(in, cout);return in;private:T* e;/ Store all elementsint size;template class LowerTria

52、ngularMatrix<double> ;#endif /* LOWERTRIANGULARMATRIX_H_ */* * LowerTriangularMatrix.cpp * * Created on: Nov 22, 2015 * Author: xudp */#include "LowerTriangularMatrix.h"#include "Exceptions.h"template<class T>LowerTriangularMatrix<T>:LowerTriangularMatrix(int si

53、ze) if (size < 1)throw InvalidParameterValue();e = new Tsize * (size + 1) / 2;this->size = size;template<class T>LowerTriangularMatrix<T>:LowerTriangularMatrix() delete e;template<class T>LowerTriangularMatrix<T>& LowerTriangularMatrix<T>:Store(int row, int co

54、lumn,const T& value) if (row < 1 | row < column | column < 1)throw InvalidParameterValue();esize * (column - 1) - 1 + row + column * (1 - column) / 2 = value;return *this;template<class T>T LowerTriangularMatrix<T>:Retrieve(int row, int column) if (row < 1 | row > size

55、 | column < 1 | column > size| (row - column) > 1 | (row - column) < -1)throw InvalidParameterValue();return esize * (column - 1) - 1 + row + column * (1 - column) / 2;template<class T>void LowerTriangularMatrix<T>:Input(istream& in, ostream& out)out << "请按

56、行主顺序依次输入元素," << endl;out << "元素个数必须恰好是" << (size * (size + 1) / 2) << "个: "<< endl;for (int i = 0; i < size; i+) for (int j = 0; j <= i; j+) T element;in >> element;Store(i + 1, j + 1, element);out << "操作成功完成. " <&

57、lt; endl;template<class T>void LowerTriangularMatrix<T>:Output(ostream& out) const for (int i = 0; i < size; i+) for (int j = 0; j < size; j+)if (j > i)out << "0t"else out << esize * j + i - j * (j + 1) / 2 << "t"out << endl;/* * HashTable.h * * Created on: Nov 23, 2015 * Author: xudp */#ifndef HASHTABLE_H_#define HASHTABLE_H_#include <iostream>using namespace std;template<class E, class K>class HashTable /* * 4、使用散列表设计实现一个字典,假设关键字为整数且 D 为 961,在字典 * 中插入随机产生的 500 个不同的整数,实现字典的建立和搜索操作。

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论