作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解

上传人:yulij****0329 文档编号:138433452 上传时间:2020-07-15 格式:PPT 页数:66 大小:537KB
返回 下载 相关 举报
作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解_第1页
第1页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解_第2页
第2页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解_第3页
第3页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解_第4页
第4页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解_第5页
第5页 / 共66页
点击查看更多>>
资源描述

《作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解》由会员分享,可在线阅读,更多相关《作为抽象数据类型的数组顺序表稀疏矩阵字符串知识讲解(66页珍藏版)》请在金锄头文库上搜索。

1、作为抽象数据类型的数组 顺序表 稀疏矩阵 字符串,第二章 数组,作为抽象数据类型的数组,一维数组 一维数组的示例,35 27 49 18 60 54 77 83 41 02,0 1 2 3 4 5 6 7 8 9,一维数组的特点,连续存储的线性表(别名 向量),数组的定义和初始化,#include class szcl int e; public: szcl ( ) e = 0; szcl ( int value ) e = value; int get_value ( ) return e; ,一维数组(Array)类的定义,#include #include template class

2、Array Type *elements; /数组存放空间 int ArraySize; /当前长度 void getArray ( ); /建立数组空间 public: Array( int Size=DefaultSize ); Array( const Array,Array( ) delete elements; Array /扩充数组 ,template void Array : getArray ( ) /私有函数:创建数组存储空间 elements = new TypeArraySize; if ( elements = NULL ) arraySize = 0; cerr “存

3、储分配错! endl; return; ,一维数组公共操作的实现,template Array : Array ( int sz ) /构造函数 if ( sz = 0 ) arraySize = 0; cerr “非法数组大小” endl; return; ArraySize = sz; getArray ( ); ,template Array : Array ( Array ,template Type 使用该函数于赋值语句 Pos = Positioni -1 + Numberi -1,template void Array : Resize ( int sz ) if ( sz =

4、0 /按新的大小确定传送元素个数,Type *srcptr = elements; /源数组指针 Type *destptr = newarray; /目标数组指针 while ( n- ) * destptr+ = * srcptr+; /从源数组向目标数组传送 delete elements; elements = newarray; ArraySize = sz; ,二维数组 三维数组,行向量 下标 i 页向量 下标 i 列向量 下标 j 行向量 下标 j 列向量 下标 k,数组的连续存储方式,一维数组,35 27 49 18 60 54 77 83 41 02,0 1 2 3 4 5

5、6 7 8 9,l l l l l l l l l l,LOC(i) = LOC(i-1)+l = a+i*l,LOC(i) =,LOC(i-1)+l = a+i*l, i 0,a, i = 0,a+i*l,a,二维数组,行优先 LOC ( j, k ) = = a + ( j * m + k ) * l,三维数组,各维元素个数为 m1, m2, m3 下标为 i1, i2, i3的数组元素的存储地址: (按页/行/列存放),LOC ( i1, i2, i3 ) = a + ( i1* m2 * m3 + i2* m3 + i3 ) * l,前i1页总 元素个数,第i1页的 前i2行总元素个数

6、,n 维数组,各维元素个数为 m1, m2, m3, , mn 下标为 i1, i2, i3, , in 的数组元素的存储地址:,LOC ( i1, i2, , in ) = a + ( i1*m2*m3*mn + i2*m3*m4*mn+ + + in-1*mn + in ) * l,线性表 (Linear List),线性表的定义和特点 定义 n( 0)个数据元素的有限序列,记作 (a1, a2, , an) ai 是表中数据元素,n 是表长度。 遍历 逐项访问: 从前向后 从后向前,线性表的特点,除第一个元素外,其他每一个元素有且仅有一个直接前驱。 除最后一个元素外,其他每一个元素有且仅

7、有一个直接后继。,顺序表 (Sequential List),顺序表的定义和特点 定义 将线性表中的元素相继存放在一个连续的存储空间中 可利用一维数组描述存储结构 特点 线性表的顺序存储方式 遍历 顺序访问,25 34 57 16 48 09,0 1 2 3 4 5,data,顺序表(SeqList)类的定义,template class SeqList Type *data; /顺序表存储数组 int MaxSize; /最大允许长度 int last; /当前最后元素下标 public: SeqList ( int MaxSize = defaultSize ); SeqList ( )

8、delete data; int Length ( ) const return last+1; ,int Find ( Type ,顺序表部分公共操作的实现,template /构造函数 SeqList : SeqList ( int sz ) if ( sz 0 ) MaxSize = sz; last = -1; data = new TypeMaxSize; if ( data = NULL ) MaxSize = 0; last = -1; return; ,template int SeqList : Find ( Type ,顺序搜索图示,25 34 57 16 48 09,0

9、1 2 3 4 5,data,搜索 16,i,25 34 57 16 48 09,i,25 34 57 16 48 09,i,25 34 57 16 48 09,i,搜索成功,25 34 57 16 48,0 1 2 3 4,data,搜索 50,i,25 34 57 16 48,i,25 34 57 16 48,i,25 34 57 16 48,i,25 34 57 16 48,i,搜索失败,搜索成功 若搜索概率相等,则 搜索不成功 数据比较 n 次,表项的插入,25 34 57 16 48 09 63 ,0 1 2 3 4 5 6 7,data,50,插入 x,25 34 57 50 16

10、 48 09 63 ,0 1 2 3 4 5 6 7,data,50,i,template int SeqList : Insert (Type /插入成功 ,表项的删除,25 34 57 50 16 48 09 63 ,0 1 2 3 4 5 6 7,data,16,删除 x,25 34 57 50 48 09 63 ,0 1 2 3 4 5 6 7,data,template int SeqList : Remove ( Type /表中没有 x ,顺序表的应用:集合的“并”运算,void Union ( SeqList ,void Intersection ( SeqList /未找到在

11、A中删除它 ,顺序表的应用:集合的“交”运算,稀疏矩阵 (Sparse Matrix),非零元素个数远远少于矩阵元素个数,稀疏矩阵的抽象数据类型 template class SparseMatrix; template class Trituple /三元组 friend class SparseMatrix private: int row, col; /非零元素行号/列号 Type value; /非零元素的值 template class SparseMatrix /稀疏矩阵类定义,int Rows, Cols, Terms; /行/列/非零元素数 Trituple smArrayMa

12、xTerms; public: /三元组表 SparseMatrix (int MaxRow, int Maxcol); SparseMatrix /相乘 ,稀疏矩阵,转置矩阵,用三元组表表示的稀疏矩阵及其转置,稀疏矩阵转置算法思想,设矩阵列数为 Cols,对矩阵三元组表扫描Cols 次。第 k 次检测列号为 k 的项。 第 k 次扫描找寻所有列号为 k 的项,将其行号变列号、列号变行号,顺次存于转置矩阵三元组表。,稀疏矩阵的转置 template SparseMatrix /转置三元组表存放指针,for ( int k = 0; k a.Cols; k+ ) /对所有列号处理一遍 for (

13、 int i = 0; i a.Terms; i+ ) if ( a.smArrayi.col = k ) b.smArrayCurrentB.row = k; b.smArrayCurrentB.col = a.smArrayi.row; b.smArrayCurrentB.value= a.smArrayi.value; CurrentB+; , return b; ,快速转置算法,建立辅助数组 rowSize 和 rowStart,记录矩阵转置后各行非零元素个数和各行元素在转置三元组表中开始存放的位置。,扫描矩阵三元组表,根据某项的列号,确定它转置后的行号,查 rowStart 表,按查

14、到的位置直接将该项存入转置三元组表中。,for ( int i = 0; i a.Cols; i+ ) rowSizei = 0; for ( i = 0; i a.Terms; i+ ) rowSizea.smArrayi.col+; rowStart0 = 0; for ( i = 1; i Cols; i+ ) rowStarti = rowStarti-1+rowSizei-1;,稀疏矩阵的快速转置 template SparseMatrix,for ( i = 0; i Terms; i+ ) rowSizesmArrayi.col+; rowStart0 = 0; for ( i = 1; i a.Cols; i+ ) rowStarti = rowStarti-1+rowSizei-1; for ( i = 0; i a.Terms; i+ ) int j = rowStarta.smArrayi.col; b.smArrayj.row = a.smArrayi.col; b.smArrayj.col = a.smArrayi.row; b.smArrayj.value = a.smArrayi.value; rowStarta.smArrayi.col+; , delete rowSize; delete rowS

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号