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
7、式,提供 store 和 retrieve 方法。
TridiagonalMatrix(int size = 10);
~TridiagonalMatrix();
// row>0, column>0
TridiagonalMatrix
8、stream& operator<< (ostream& out, const TridiagonalMatrix
9、class TridiagonalMatrix
10、 (size < 1)
throw InvalidParameterValue();
e = new T[3 * size - 2];
this->size = size;
}
template
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
12、 column < 1 || column > size
|| (row - column) > 1 || (row - column) < -1)
throw InvalidParameterValue();
return e[2 * column + row - 3];
}
template
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
15、eMatrix.h
*
* Created on: Oct 20, 2015
* Author: xudp
*/
#ifndef SPARSEMATRIX_H_
#define SPARSEMATRIX_H_
#include
16、 value;
};
template
17、id Add(const SparseMatrix
18、ream& in, ostream& out);
void Output(ostream& out) const;
friend ostream& operator<<(ostream& out, const SparseMatrix
19、t GetMaxSize(){
return MaxTerms;
}
private:
void Append(const Term
20、TRIX_H_ */
/*
* SparseMatrix.cpp
*
* Created on: Oct 20, 2015
* Author: xudp
*/
#include "SparseMatrix.h"
#include "Exceptions.h"
template
21、
MaxTerms = maxTerms;
a = new Term
22、
do {
cursor++;
if (cursor == terms) {
Term
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
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
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 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 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 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 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 37、n: Nov 9, 2015
* Author: xudp
*/
#ifndef SORTEDCHAIN_H_
#define SORTEDCHAIN_H_
#include 38、 SortedChain;
template 39、 return first == 0;
}
int Length() const;
bool Search(const K& k, E& e) const;
SortedChain 40、t, const SortedChain 41、 Author: xudp
*/
#include "SortedChain.h"
#include "Exceptions.h"
template 42、K>
int SortedChain 43、if no match.
SortedChainNode 44、 SortedChain 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 47、 p->link;
} else {
SortedChainNode 48、return *this;
}
template 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 50、ter tp
q->link = p;
if (tp)
tp->link = q;
else
first = q;
return *this;
}
template






