作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt

上传人:sccc 文档编号:5782622 上传时间:2023-08-19 格式:PPT 页数:66 大小:600.55KB
返回 下载 相关 举报
作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt_第1页
第1页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt_第2页
第2页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt_第3页
第3页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt_第4页
第4页 / 共66页
作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt_第5页
第5页 / 共66页
点击查看更多>>
资源描述

《作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt》由会员分享,可在线阅读,更多相关《作为抽象数据类型的数组顺序表稀疏矩阵字符串.ppt(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;,main()szcl a13=3,5,7,*elem;for(int i=0;i get_value()“n”;/动态 elem+;retu

2、rn 0;,一维数组(Array)类的定义,#include#include template class 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=NUL

3、L)arraySize=0;cerr“存储分配错!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=0/按新的大小确定传送元素个数,Type*srcptr=e

4、lements;/源数组指针 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 6 7 8 9,l l l l l l l l l l,LOC(i)=LOC(i-1)+l=a+i*l,LOC

5、(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行总元素个数,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,线性

6、表(Linear List),线性表的定义和特点 定义 n(0)个数据元素的有限序列,记作(a1,a2,an)ai 是表中数据元素,n 是表长度。遍历 逐项访问:从前向后 从后向前,线性表的特点,除第一个元素外,其他每一个元素有且仅有一个直接前驱。除最后一个元素外,其他每一个元素有且仅有一个直接后继。,顺序表(Sequential List),顺序表的定义和特点 定义 将线性表中的元素相继存放在一个连续的存储空间中 可利用一维数组描述存储结构 特点 线性表的顺序存储方式 遍历 顺序访问,25 34 57 16 48 09,0 1 2 3 4 5,data,顺序表(SeqList)类的定义,te

7、mplate class SeqList Type*data;/顺序表存储数组 int MaxSize;/最大允许长度 int last;/当前最后元素下标public:SeqList(int MaxSize=defaultSize);SeqList()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)Max

8、Size=0;last=-1;return;,template int SeqList:Find(Type,顺序搜索图示,25 34 57 16 48 09,0 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,搜索失败,搜索成功 若搜索概率相等,则搜索不成功

9、数据比较 n 次,表项的插入,25 34 57 16 48 09 63,0 1 2 3 4 5 6 7,data,50,插入 x,25 34 57 50 16 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,顺序表的应用

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

11、ows,Cols,Terms;/行/列/非零元素数 Trituple smArrayMaxTerms;public:/三元组表 SparseMatrix(int MaxRow,int Maxcol);SparseMatrix/相乘,稀疏矩阵,转置矩阵,用三元组表表示的稀疏矩阵及其转置,稀疏矩阵转置算法思想,设矩阵列数为 Cols,对矩阵三元组表扫描Cols 次。第 k 次检测列号为 k 的项。第 k 次扫描找寻所有列号为 k 的项,将其行号变列号、列号变行号,顺次存于转置矩阵三元组表。,稀疏矩阵的转置 template SparseMatrix/转置三元组表存放指针,for(int k=0;k

12、 a.Cols;k+)/对所有列号处理一遍 for(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 表,

13、按查到的位置直接将该项存入转置三元组表中。,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

14、;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 rowStart;return b;,字符串(String),字符串是 n(0)个字符的有限序列,记作 S:“c1c2c3cn”其中,S 是串名字“c1c2c3cn”是串值 ci 是串中字符 n 是串的长度。例如,S

15、=“Tsinghua University”,const int maxLen=128;class String int curLen;/串的当前长度 char*ch;/串的存储数组 public:String(const String,字符串抽象数据类型和类定义,int Length()const return curLen;/求当前串*this的实际长度 String/判当前串*this与对象串ob是否不等,int operator!()const return curLen=0;/判当前串*this是否空串 String,String:String(const String/复制串值,字

16、符串部分操作的实现,String:String(const char*init)/复制构造函数:从已有字符数组*init复制 ch=new charmaxLen+1;/创建串数组 if(ch=NULL)cerr“存储分配错!n”;exit(1);curLen=strlen(init);/复制串长度 strcpy(ch,init);/复制串值,String:String()/构造函数:创建一个空串 ch=new charmaxLen+1;/创建串数组 if(ch=NULL)cerr“存储分配错!n”;exit(1);curLen=0;ch0=0;,提取子串的算法示例,pos+len-1 pos+

17、len-1 curLen-1 curLen,i n f i n i t y,i n f i n i t y,pos=2,len=3,pos=5,len=4,f i n,i t y,超出,String,temp-curLen=len;/子串长度 for(int i=0,j=pos;i chi=chj;/传送串数组 temp-chlen=0;/子串结束 return*temp;,例:串 st=“university”,pos=3,len=4使用示例 subSt=st(3,4)提取子串 subSt=“vers”,String,char数组赋值 newSt=“university”提取字符 newCh

18、ar=n,String,例:串 st1=“beijing”,st2=“university”,使用示例 st1+=st2;连接结果 st1=“beijing university”st2=“university”,串的模式匹配,定义 在串中寻找子串(第一个字符)在串中的位置词汇 在模式匹配中,子串称为模式,串称为目标。示例 目标 T:“Beijing”模式 P:“jin”匹配结果=3,第1趟 T a b b a b a 穷举的模式 P a b a 匹配过程 第2趟 T a b b a b a P a b a 第3趟 T a b b a b a P a b a第4趟 T a b b a b a P a b a,int String:Find(String/pat扫描完,匹配成功,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号