ImageVerifierCode 换一换
格式:DOCX , 页数:38 ,大小:3.35MB ,
资源ID:8396506      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/8396506.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(山东大学-数据结构实验报告-矩阵及散列表.docx)为本站上传会员【仙人****88】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

山东大学-数据结构实验报告-矩阵及散列表.docx

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

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

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

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

5、写了有序链表类SortedChain、开放寻址的散列表类HashTable、基于有序链表链接的散列表类ChainHashTable,并对这三个类分别扩展编写了Output函数。 3. 测试结果(测试输入,测试输出) 问题一: 问题二: 上图显示了输入不符合下三角方阵约束时,抛出异常并退出程序。 上图是正常运行的结果。 问题三: 普通的输入和输出操作如下: 矩阵相加: 矩阵转置: 问题四: 以上图的数据为例。从346就应该在链表链接的散列表上看到开放寻址解决冲突的例子。返回开放寻址的输出段,可以看到符合预期的结果: 4.

6、实现源代码(程序风格清晰易理解,有充分的注释) /* * TridiagonalMatrix.h * * Created on: Nov 22, 2015 * Author: xudp */ #ifndef TRIDIAGONALMATRIX_H_ #define TRIDIAGONALMATRIX_H_ #include using namespace std; template class TridiagonalMatrix { public: // 1、创建三对角矩阵类,采用按列映射方

7、式,提供 store 和 retrieve 方法。 TridiagonalMatrix(int size = 10); ~TridiagonalMatrix(); // row>0, column>0 TridiagonalMatrix& 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 o

8、stream& operator<< (ostream& out, const TridiagonalMatrix& matrix){ matrix.Output(out); return out; } friend istream& operator>> (istream& in, TridiagonalMatrix& matrix){ matrix.Input(in, cout); return in; } private: T* e; // Store all elements int size; }; template

9、class TridiagonalMatrix; #endif /* TRIDIAGONALMATRIX_H_ */ /* * TridiagonalMatrix.cpp * * Created on: Nov 22, 2015 * Author: xudp */ #include "TridiagonalMatrix.h" #include "Exceptions.h" template TridiagonalMatrix::TridiagonalMatrix(int size) { if

10、 (size < 1) throw InvalidParameterValue(); e = new T[3 * size - 2]; this->size = size; } template TridiagonalMatrix::~TridiagonalMatrix() { delete[] e; } template TridiagonalMatrix& TridiagonalMatrix::Store(int row, int column, const T& value) { if

11、row < 1 || row > size || column < 1 || column > size || (row - column) > 1 || (row - column) < -1) throw InvalidParameterValue(); e[2 * column + row - 3] = value; return *this; } template T TridiagonalMatrix::Retrieve(int row, int column) { if (row < 1 || row > size ||

12、 column < 1 || column > size || (row - column) > 1 || (row - column) < -1) throw InvalidParameterValue(); return e[2 * column + row - 3]; } template void TridiagonalMatrix::Input(istream& in, ostream& out) { out << "请按行主顺序依次输入元素," << endl; out << "元素个数必须恰好是" << (3 * siz

13、e - 2) << "个: " << endl; for (int i = 0; i < size; i++) { for (int j = i - 1; j <= i + 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 << "操作成功完成. " << e

14、ndl; } template void TridiagonalMatrix::Output(ostream& out) const { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) if ((i - j) > 1 || (i - j) < -1) out << "0\t"; else { out << e[2 * j + i] << "\t"; } out << endl; } } /* * Spars

15、eMatrix.h * * Created on: Oct 20, 2015 * Author: xudp */ #ifndef SPARSEMATRIX_H_ #define SPARSEMATRIX_H_ #include using namespace std; template class SparseMatrix; template class Term { friend SparseMatrix ; private: int row, col; T

16、 value; }; template class SparseMatrix { public: /* 3、创建稀疏矩阵类,采用行主顺序把稀疏矩阵映射到一维数组中,实现稀 * 疏矩阵的转置和两个稀疏矩阵的加法操作。 */ SparseMatrix(int maxTerms = 10); SparseMatrix(const SparseMatrix& spm); ~SparseMatrix() { delete[] a; } void Transpose(SparseMatrix &b) const; vo

17、id Add(const SparseMatrix &b, SparseMatrix &c) const; /* * Write the store and retrieve functions for a sparse matrix stored in * row-major order in a one-dimensional array. */ SparseMatrix& Store(const T& x, int i, int j); T Retrieve(int i, int j) const; void Input(ist

18、ream& in, ostream& out); void Output(ostream& out) const; friend ostream& operator<<(ostream& out, const SparseMatrix& matrix) { matrix.Output(out); return out; } friend istream& operator>>(istream& in, SparseMatrix& matrix) { matrix.Input(in, cout); return in; } in

19、t GetMaxSize(){ return MaxTerms; } private: void Append(const Term& t); int rows, cols; // matrix dimensions int terms; // current number of nonzero terms Term * a; // term array int MaxTerms; // size of array a }; template class SparseMatrix ; #endif /* SPARSEMA

20、TRIX_H_ */ /* * SparseMatrix.cpp * * Created on: Oct 20, 2015 * Author: xudp */ #include "SparseMatrix.h" #include "Exceptions.h" template SparseMatrix::SparseMatrix(int maxTerms) { // Sparse matrix constructor. if (maxTerms < 1) throw BadInitializers();

21、 MaxTerms = maxTerms; a = new Term [MaxTerms]; terms = cols = rows = 0; } template SparseMatrix& SparseMatrix::Store(const T& theVal, int theRow, int theCol) { if (theRow < 1 || theCol < 1 || theRow > rows || theCol > cols) throw OutOfBounds(); int cursor = -1;

22、 do { cursor++; if (cursor == terms) { Term t; t.row = theRow; t.col = theCol; t.value = theVal; Append(t); return *this; } } while (a[cursor].row < theRow || (a[cursor].row == theRow && a[cursor].col < theCol)); if (a[cursor].row == theRow && a[cursor

23、].col == theCol) // (theRow,theCol) is already existent. a[cursor].value = theVal; else { if (terms >= MaxTerms) throw NoMem(); for (int k = terms - 1; k >= cursor; k--) { a[k + 1] = a[k]; } a[cursor].row = theRow; a[cursor].col = theCol; a[cursor].value = theVal; t

24、erms++; } return *this; } template T SparseMatrix::Retrieve(int theRow, int theCol) const { if (theRow < 1 || theCol < 1 || theRow > rows || theCol > cols) throw OutOfBounds(); int cursor = 0; while (a[cursor].row < theRow || (a[cursor].row == theRow && a[cursor].co

25、l < theCol)) { cursor++; if (cursor == terms) // not in the sparse matrix return 0; } if (a[cursor].row == theRow && a[cursor].col == theCol) // (theRow,theCol) is already existent. return a[cursor].value; else return 0; } template void SparseMatrix::Output(o

26、stream& out) const { out << "最大容许行数:" << rows << " 最大容许列数:" << cols << " 非零元素数:" << terms << endl; // put terms, one per line for (int i = 0; i < terms; i++) out << "a(" << a[i].row << ',' << a[i].col << ") = " << a[i].value << endl; } template void SparseMatrix

27、>::Input(istream& in, ostream& out) { out << "分别输入行数最大值,列数最大值,以及本次输入的元素组数:"; int theTerms; in >> rows >> cols >> theTerms; if (theTerms > MaxTerms) throw NoMem(); // input terms int theRow, theCol; T theVal; for (int i = 0; i < theTerms; i++) { out << "依次输入第" << (i + 1) << "项

28、的所在的行、列,以及它对应的数值"; in >> theRow >> theCol >> theVal; Store(theVal, theRow, theCol); } } template void SparseMatrix::Transpose(SparseMatrix &b) const { // Return transpose of *this in b. // make sure b has enough space if (terms > b.MaxTerms) throw NoMem();

29、// set transpose characteristics b.cols = rows; b.rows = cols; b.terms = terms; // initialize to compute transpose int *ColSize, *RowNext; ColSize = new int[cols + 1]; RowNext = new int[rows + 1]; // find number of entries in each column of *this for (int i = 1; i <= cols; i++)

30、 // initialize ColSize[i] = 0; for (int i = 0; i < terms; i++) ColSize[a[i].col]++; // find the starting point of each row of b RowNext[1] = 0; for (int i = 2; i <= cols; i++) RowNext[i] = RowNext[i - 1] + ColSize[i - 1]; // perform the transpose copying from *this to b for

31、 (int i = 0; i < terms; i++) { int j = RowNext[a[i].col]++; // position in b b.a[j].row = a[i].col; b.a[j].col = a[i].row; b.a[j].value = a[i].value; } } template void SparseMatrix::Add(const SparseMatrix &b, SparseMatrix &c) const { // Compute c = (*this) +

32、b. // 矩阵规模匹配 if (rows != b.rows || cols != b.cols) throw SizeMismatch(); c.cols = cols; c.rows = rows; // 重新初始化稀疏矩阵c delete[] c.a; int newMaxTerms = b.terms + terms; c.MaxTerms = (newMaxTerms > c.MaxTerms) ? newMaxTerms : c.MaxTerms; c.a = new Term [c.MaxTerms]; // Not

33、the best way. bool* hasMatched_b = new bool[b.terms]; for (int i = 0; i < b.terms; i++) hasMatched_b[i] = false; int curIndex = 0; for (int i = 0; i < terms; i++) { bool isMatched = false; for (int j = 0; j < b.terms; j++) { if (a[i].row == b.a[j].row && a[i].col == b.a[j].col)

34、{ isMatched = true; hasMatched_b[j] = true; c.a[curIndex].row = a[i].row; c.a[curIndex].col = a[i].col; c.a[curIndex].value = a[i].value + b.a[j ].value; break; } } if (!isMatched) { c.a[curIndex].row = a[i].row; c.a[curIndex].col = a[i].col; c.a[c

35、urIndex].value = a[i].value; } curIndex++; } for (int i = 0; i < b.terms; i++) { if (!hasMatched_b[i]) { c.a[curIndex].row = b.a[i].row; c.a[curIndex].col = b.a[i].col; c.a[curIndex].value = b.a[i].value; curIndex++; } } // When writing this function by myself,

36、I forgot the following: c.terms = curIndex; delete[] hasMatched_b; } template void SparseMatrix::Append(const Term& t) { // Append a nonzero term t to *this if (terms >= MaxTerms) throw NoMem(); a[terms] = t; terms++; } /* * SortedChain.h * * Created o

37、n: Nov 9, 2015 * Author: xudp */ #ifndef SORTEDCHAIN_H_ #define SORTEDCHAIN_H_ #include using namespace std; /* template note: E denotes the data type of the chain elements, and K * that of the keys on which the chain is sorted. */ template class

38、 SortedChain; template class SortedChainNode { friend class SortedChain ; private: E data; SortedChainNode *link; }; template class SortedChain { public: SortedChain() { first = 0; } ~SortedChain(); bool IsEmpty() const {

39、 return first == 0; } int Length() const; bool Search(const K& k, E& e) const; SortedChain& Delete(const K& k, E& e); SortedChain& Insert(const E& e); SortedChain& DistinctInsert(const E& e); void Output(ostream& out) const; friend ostream& operator<< (ostream& ou

40、t, const SortedChain& sc){ sc.Output(out); return out; } private: SortedChainNode *first; }; template class SortedChainNode ; template class SortedChain ; #endif /* SORTEDCHAIN_H_ */ /* * SortedChain.cpp * * Created on: Nov 9, 2015 *

41、 Author: xudp */ #include "SortedChain.h" #include "Exceptions.h" template SortedChain::~SortedChain() { SortedChainNode* p = first; SortedChainNode* tmp; while (p) { tmp = p; p = p->link; delete tmp; } } template

42、K> int SortedChain::Length() const { SortedChainNode *p = first; int ret = 0; while (p) { ret++; p = p->link; } return ret; } template bool SortedChain::Search(const K& k, E& e) const { // Put element that matches k in e. // Return false

43、if no match. SortedChainNode *p = first; // search for match with k for (; p && p->data < k; p = p->link) ; // verify match if (p && p->data == k) // yes, found match { e = p->data; return true; } return false; } template SortedChain&

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

45、 // verify match if (p && p->data == k) { // found a match e = p->data; // save data // remove p from chain if (tp) tp->link = p->link; else first = p->link; // p is first node. delete p; return *this; } throw BadInput(); // no match } template

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

47、 p->link; } else { SortedChainNode* ncn = new SortedChainNode(); ncn->data = e; ncn->link = p->link; p->link = ncn; break; } } if (!p->link) { p->link = new SortedChainNode(); p = p->link; p->link = 0; p->data = e; } }

48、return *this; } template SortedChain& SortedChain::DistinctInsert(const E& e) { // Insert e only if no element with same key // currently in list. // Throw BadInput exception if duplicate. SortedChainNode *p = first, *tp = 0; // trail p // move

49、tp so that e can be inserted after tp for (; p && p->data < e; tp = p, p = p->link) ; // check if duplicate if (p && p->data == e) throw BadInput(); // not duplicate, set up node for e SortedChainNode *q = new SortedChainNode; q->data = e; // insert node just af

50、ter tp q->link = p; if (tp) tp->link = q; else first = q; return *this; } template void SortedChain::Output(ostream& out) const{ SortedChainNode *p = first; if (!first) out << "(Empty chain.)"; else while(p){ out << p->data << "\t";

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服