数据结构与算法.ppt

上传人:本田雅阁 文档编号:3185605 上传时间:2019-07-22 格式:PPT 页数:67 大小:413.51KB
返回 下载 相关 举报
数据结构与算法.ppt_第1页
第1页 / 共67页
数据结构与算法.ppt_第2页
第2页 / 共67页
数据结构与算法.ppt_第3页
第3页 / 共67页
数据结构与算法.ppt_第4页
第4页 / 共67页
数据结构与算法.ppt_第5页
第5页 / 共67页
点击查看更多>>
资源描述

《数据结构与算法.ppt》由会员分享,可在线阅读,更多相关《数据结构与算法.ppt(67页珍藏版)》请在三一文库上搜索。

1、数据结构与算法,2006.9-2007.1,作为抽象数据类型的数组,一维数组 一维数组的示例,一维数组的特点,连续存储的线性聚集(别名 向量) 除第一个元素外,其他每一个元素有一个且仅有一个直接前驱。 除最后一个元素外,其他每一个元素有一个且仅有一个直接后继。,数组的定义和初始化,#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=

2、0, i3, i+ ) cout a1i.get_value ( ) “n”; /打印静态数组 elem = ,一维数组(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

3、 ( ) /私有函数:创建数组存储空间 elements = new TypeArraySize; if ( elements = 0 ) cerr “Memory Allocation Error“ endl; ,一维数组公共操作的实现,template void Array:Array ( int sz ) /构造函数 if ( sz = 0 ) cerr “Invalid Array Size“ endl; return; ArraySize = sz; getArray ( ); ,template Array: Array ( const Array ,template Type 使

4、用该函数于赋值语句 Positioni = Positioni -1 + Numberi -1,template void Array:Resize (int sz) if ( sz = 0 ,二维数组 三维数组,行向量 下标 i 页向量 下标 i 列向量 下标 j 行向量 下标 j 列向量 下标 k,数组的连续存储方式,一维数组,LOC ( i ) = LOC ( i -1 ) + l =+ i*l,二维数组,行优先 LOC ( j, k ) = j * m + k,n维数组,各维元素个数为 m1, m2, m3, , mn 下标为 i1, i2, i3, , in 的数组元素的存储地址:,

5、顺序表 (Sequential List),顺序表的定义和特点 定义 n( 0)个表项的有限序列 (a1, a2, , an) ai是表项,n是表长度。 特点 顺序存取 遍历 逐项访问 从前向后 从后向前 从两端向中间,顺序表(SeqList)类的定义,template class SeqList Type *data; /顺序表存储数组 int MaxSize; /最大允许长度 int last; /当前最后元素下标 public: SeqList ( int MaxSize = defaultSize ); SeqList ( ) delete data; int Length ( ) c

6、onst return last+1; int Find ( Type ,int IsIn ( Type ,顺序表部分公共操作的实现,template SeqList:SeqList ( int sz ) /构造函数 if ( sz 0 ) MaxSize = sz; last = -1; data = new TypeMaxSize; ,template int SeqList:Find ( Type ,顺序搜索图示,x = 48 x = 50,搜索成功 若搜索概率相等,则 搜索不成功 数据比较 n 次,表项的插入,template int SeqList:Insert ( Type /插入

7、成功 ,表项的删除,template int SeqList:Remove ( Type /表中没有 x ,顺序表的应用:集合的“并”运算,template void Union ( SeqList ,template void Intersection ( SeqList /未找到在LA中删除它 ,顺序表的应用:集合的“交”运算,多项式 (Polynomial),n阶多项式Pn(x)有n+1项。 系数 a0, a1, a2, , an 指数 0, 1, 2, , n。按升幂排列,class Polynomial public: Polynomial ( ); /构造函数 int operat

8、or ! ( ); /判是否零多项式 Coefficient Coef (Exponent e); Exponent LeadExp ( ); /返回最大指数 Polynomial Add (Polynomial poly); Polynomial Mult (Polynomial poly); float Eval ( float x); /求值 ,多项式(Polynomial)的抽象数据类型,#include class power double x; int e; double mul; public: power (double val, int exp); double get_po

9、wer ( ) return mul; ;,创建power类,计算x的幂,power:power (double val, int exp) /按val值计算乘幂 x = val; e = exp; mul = 1.0; if (exp = 0 ) return; for ( ; exp0; exp-) mul = mul * x; main ( ) power pwr ( 1.5, 2 ); cout pwr.get_power ( ) “n”; ,多项式的存储表示,第一种: private: (静态数 int degree; 组表示) float coef maxDegree+1; Pn(

10、x)可以表示为: pl.degree = n pl.coefi = ai, 0 i n,第二种:private: (动态数 int degree; 组表示) float * coef; Polynomial:Polynomial (int sz) degree = sz; coef = new float degree + 1; 以上两种存储表示适用于指数连续排列的多项式。但对于指数不全的多项式如P101(x) = 3 + 5x50 - 14x101, 不经济。,第三种: class Polynomial; class term /多项式的项定义 friend Polynomial; priv

11、ate: float coef; /系数 int exp; /指数 ;,class Polynomial /多项式定义 public: private: static term termArrayMaxTerms; /项数组 static int free; /当前空闲位置指针 / term Polynomial:termArrayMaxTerms; / int Polynomial:free = 0; int start, finish; /多项式始末位置 ,A(x) = 2.0x1000+1.8 B(x) = 1.2 + 51.3x50 + 3.7x101,两个多项式存放在termArra

12、y中,两个多项式的相加,结果多项式另存 扫描两个相加多项式,若都未检测完: 若当前被检测项指数相等,系数相加。若未变成 0,则将结果加到结果多项式。 若当前被检测项指数不等,将指数小者加到结果多项式。 若有一个多项式已检测完,将另一个多项式剩余部分复制到结果多项式。,Polynomial Polynomial:Add ( Polynomial B ) Polynomial C; int a = start; int b = B.start; C.start = free; float c; while ( a = finish ,case : NewTerm ( termArrayb.coef

13、, termArrayb.exp ); b+; break; case : NewTerm ( termArraya.coef, termArraya.exp ); a+; for ( ; a=finish; a+ ) /a未检测完时 NewTerm ( termArraya.coef, termArraya.exp );,for ( ; b=B.finish; b+ ) /b未检测完时 NewTerm ( termArrayb.coef, termArrayb.exp ); C.finish = free-1; return C; ,在多项式中加入新的项 void Polynomial:Ne

14、wTerm ( float c, int e ) /把一个新的项加到多项式C(x)中 if ( free = maxTerms ) cout “Too many terms in polynomials” endl; return; termArrayfree.coef = c; termArrayfree.exp = e; free+; ,稀疏矩阵 (Sparse Matrix),行数m = 6, 列数n = 7, 非零元素个数t = 6,稀疏矩阵(SparseMatrix)的抽象数据类型 template class SparseMatrix int Rows, Cols, Terms;

15、/行/列/非零元素数 Trituple smArrayMaxTerms; public: /三元组表 SparseMatrix ( int MaxRow, int Maxcol ); SparseMatrix Transpose ( ); /转置 SparseMatrix /相加 Add ( SparseMatrix b ); SparseMatrix /相乘 Multiply ( SparseMatrix b ); ,三元组 (Trituple) 类的定义 template class SparseMatrix; template class Trituple friend class Sp

16、arseMatrix private: int row, col; /非零元素所在行号/列号 Type value; /非零元素的值 ,稀疏矩阵,转置矩阵,用三元组表表示的稀疏矩阵及其转置,稀疏矩阵转置算法思想,设矩阵列数为Cols,对矩阵三元组表扫描Cols次。第k次检测列号为k的项。 第k次扫描找寻所有列号为k的项,将其行号变列号、列号变行号,顺次存于转置矩阵三元组表。 设矩阵三元组表总共有Terms项,其时间代价为 O ( Cols* Terms )。 若矩阵有200行,200列,10,000个非零元素,总共有2,000,000次处理。,稀疏矩阵的转置 template SparseMa

17、trix SparseMatrix: Transpose ( ) SparseMatrix b; b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; /转置矩阵的列数,行数和非零元素个数 if ( Terms 0 ) int CurrentB = 0; /转置三元组表存放指针,for ( int k=0; kCols; k+ ) for ( int i=0; iTerms; i+ ) if ( smArrayi.col = k ) b.smArrayCurrentB.row = k; b.smArrayCurrentB.col = smArrayi.r

18、ow; b.smArrayCurrentB.value= smArrayi.value; CurrentB+; return b; ,快速转置算法,建立辅助数组rowSize和rowStart,记录矩阵转置后各行非零元素个数和各行元素在转置三元组表中开始存放位置。 扫描矩阵三元组表,根据某项的列号,确定它转置后的行号,查rowStart表,按查到的位置直接将该项存入转置三元组表中。 转置时间代价为 O(max(Terms, Cols)。若矩阵有200列,10000个非零元素,总共需10000次处理。,for ( int i=0; iCols; i+ ) rowSizei = 0; for (

19、i=0; iTerms; i+ ) rowSizesmArrayi.col+; rowStart0 = 0; for ( i=1; i Cols; i+ ) rowStarti = rowStarti-1+rowSizei-1;,稀疏矩阵的快速转置 template SparseMatrix SparseMatrix:FastTranspos ( ) int *rowSize = new intCols; int *rowStart = new intCols; SparseMatrix b; b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; if

20、( Terms 0 ) for (int i=0; iCols; i+) rowSizei = 0; for ( i=0; iTerms; i+ ) rowSizesmArrayi.col+;,rowStart0 = 0; for ( i=1; i Cols; i+ ) rowStarti = rowStarti-1+rowSizei-1; for ( i=0; iTerms; i+ ) int j = rowStartsmArrayi.col; b.smArrayj.row = smArrayi.col; b.smArrayj.col = smArrayi.row; b.smArrayj.v

21、alue = smArrayi.value; rowStartsmArrayi.col+; delete rowSize; delete rowStart; return b; ,字符串 (String),字符串是n ( 0 ) 个字符的有限序列,记作 S : “c1c2c3cn” 其中,S是串名字 “c1c2c3cn”是串值 ci是串中字符 n是串的长度。,const int maxLen = 128; class String int curLen; /串的当前长度 char *ch; /串的存储数组 public: String ( const String ,字符串抽象数据类型和类定义

22、,String ,String:String ( const String ,字符串部分操作的实现,String:String ( const char *init ) /复制构造函数: 从已有字符数组*init复制 ch = new charmaxLen+1; if ( !ch ) cout “Allocation Errorn”; exit(1); curLen = strlen (init); strcpy ( ch, init ); ,String:String ( ) /构造函数:创建一个空串 ch = new charmaxLen+1; if ( !ch ) cout “Alloc

23、ation Errorn”; exit(1); curLen = 0; ch0 = 0; ,提取子串的算法示例,pos+len -1 pos+len -1 curLen-1 curLen,String /动态分配,if ( pos+len -1 = curLen ) len = curLen - pos; tempcurLen = len; /子串长度 for ( int i=0, j=pos; ilen; i+, j+ ) tempchi = chj; /传送串数组 tempchlen = 0; /子串结束 return temp; ,String ,else cout = curLen ) cout “Out Of Boundary!n ”; exit (1) ; return chi; ,String ,

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1