收藏 分享(赏)

线性表顺序表 稀疏矩阵字符串.ppt

上传人:Facebook 文档编号:3506804 上传时间:2018-11-08 格式:PPT 页数:114 大小:376.50KB
下载 相关 举报
线性表顺序表 稀疏矩阵字符串.ppt_第1页
第1页 / 共114页
线性表顺序表 稀疏矩阵字符串.ppt_第2页
第2页 / 共114页
线性表顺序表 稀疏矩阵字符串.ppt_第3页
第3页 / 共114页
线性表顺序表 稀疏矩阵字符串.ppt_第4页
第4页 / 共114页
线性表顺序表 稀疏矩阵字符串.ppt_第5页
第5页 / 共114页
点击查看更多>>
资源描述

1、线性表 顺序表 稀疏矩阵 字符串,第二章 数组,一、线性表,线性表 相同数据类型的元素的有限序列叫线性表。(a1, a2, ,an-1, an) a1为首元,an为末元, n叫线性表的长度 ai的后继是ai+1, i=1, ,n-1. an没有后继。 ai的前驱是ai-1, i=2, ,n. a1没有前驱。 ai可以是基本数据类型也可以是struct 类型。 没有数据的线性表叫空表。空表的长度n=0。,a1,a2,a3,a4,a5,a6,线性表是最简单的也是最基本的数据结构。,线性表可以用来构造字符串,集合,栈,队列,用来排序。线性表可以顺序表示用一组地址连续的存储单元一次存储数据元素。线性表

2、也可以用线性链表表示。,二、顺序表线性表的顺序表示,可以用通用数组定义通用线性表。通用数组是可变长度的数组,也叫安全数组。,类模版 通用数据类型 /array.h 例. 通用数组 抽象数组类型template class Array T *alist; /指针数据 表示一个数组int size; /表示数组长度 public:Array(int s=50) /构造函数Array(const Array/输出操作重载;,#include ,template class SeqList Arraylistitem; /list storage array int size;public:SeqLi

3、st(void); / constructor构造函数/ list access methods 线性表的访问操作int ListSize(void) const; /取线性表的长int ListEmpty(void)const; /问表是否空表int Find (T /取线性表中元素,/ list modification methods线性表的修改操作,void Insert(const T,/ constructor. set size to 0,template SeqList:SeqList(void): listitem(size),size(0) ,/ return number

4、 of elements in list,template int SeqList:ListSize(void) const return size; ,/ tests for an empty list,template int SeqList:ListEmpty(void) const return size = 0; ,/ clears list by setting size to 0,template void SeqList:ClearList(void) size = 0; ,/ Take item as key and search the list. /return True

5、 if item is in the list and,/ false otherwise. If found, assign the list / element to the reference parameter item.template int SeqList:Find(T / return False when list empty,while(isize &!(item=listitemi),i+;if (i size) item = listitemi; / assign list element to itemreturn 1; / return Trueelsereturn

6、 0; / return false ,/ insert item at the rear of the list.,template void SeqList:Insert(const T / increment list size,template /在第i位插入,void SeqList:Insert(const T,/shift the tail of the list /to the right one position,while (k = i) listitemk+1 = listitemk;k-;listitemi = item;size+; / increament list

7、 size ,/search for item in the list /and delete it if found,template void SeqList:Delete(const Tif (i size) / successful if i size,/ shift the tail of the list /to the left one position,while (i size-1)listitemi = listitemi+1;i+;size-; / decreament size ,/delete element at front of list and return /

8、 its value. terminate the program with / an error message if the list is empty.,templateT SeqList:DeleteFront(void) T frontItem;/ list is empty if size = 0if (size = 0) cerr “Attempt to delete the front /of an empty list!“ endl;exit(1);,frontItem = listitem0; / get value from position 0.Delete(front

9、Item); / delete the first item and shift termsreturn frontItem; / return the original value ,/ return value at position pos in list. / if pos is not valid list position, / teminate program with an error message. template T SeqList:GetData(int pos) const / terminate program if pos out of rangeif (pos

10、 = size)cerr “pos is out of range!“ endl;exit(1);return listitempos; ,测试,#include “ iostream.h”#include “aseqlist.h” void main(void) SeqList a,b; int x;for(int i=0;ix; b.Insert(x+1);for(i=0;i20;i+)couta.GetData(i) ;coutendl;,for(i=0;i20;i+)coutb.GetData(i) ;coutendl;a.Insert(99,0); a.Insert(98,10);a

11、.Insert(97,20); a.Insert(96,30);int k=a.ListSize( );for(i=0;ik;i+)couta.GetData(i) ;coutendl; ,顺序表应用的例:,1. 用顺序表做插入,选择排序.2. 合并两个已排序的线性表3. 用顺序表做集合: 只插入不重复的元素4. 求两个集合的交合并5. 表示一个多项式6. 求多项式的和差积商,微分,积分7. 存储一个矩阵, 求矩阵的和,积,逆,顺序线性表复杂度分析,从已建立的顺序表中“取表长” ,“取第i个元素”的复杂性是O(1), 2. 在表长n的顺序表第i位前插入一个元素,要把表尾n-i+1个元素后移一格

12、。假设每一位都插入一个数,从第1位到末尾第n+1位,总移动次数是:i=1i=n+1(n-i+1)=1+2+n=n*(n+1)/2平均复杂度为O(n/2). 3. 删除第i位元素,要把表尾n-i个元素前移一格。假设每一位都删除一个数,从第1位到末尾第n位,总移动次数是:i=1i=n+1(n-i)=1+2+(n-1)=n*(n-1)/2平均复杂度为O(n-1)/2).,一个阶数很高的矩阵中如果有许多相等的元素,或零元素,成为特殊矩阵。对特殊矩阵可以进行压缩存储,三、稀疏矩阵,特殊矩阵,对称矩阵上(下)三角矩阵对角矩阵稀疏矩阵,对称矩阵,矩阵 A=(aij)n,naij= aji , 0i,jn每一

13、对元素分配一个存储空间,可以将n2个元素存储到n(n+1)/2个空间中,下三角矩阵,a11a21a22a31a32 a33an1an2 an3 ann,1+2+3+ +n=n(n+1)/2 T sn(n+1)/2;,k=i(i-1)/2+j-1; ij; k=j(j-1)/2+i-1; ij.s12=aij=aji; i=4, j=3,s0=a11; s1=a21; sk=aij,对角矩阵,a11 a12a21 a22 a23 an-1nann-1 ann,sk=aij; k=3(i-1)+j; j=k%3; i=(k-j)/3,稀疏矩阵 Sparse Matrix,m*n阶矩阵 A=(aij

14、)mn 中有t个非零元素, 如果=t/(m*n)5%, 称A为稀疏矩阵,0 12 9 0 0 0 00 0 0 0 0 0 0-3 0 0 0 0 14 00 0 24 0 0 0 00 18 0 0 0 0 015 0 0 -7 0 0 0,/smatrix.dat,6 7 8 1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,稀疏矩阵的存储,row col elem1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,template class Triple public: int row, co

15、l; T elem;SetVal(int r,int c,T d) elem=d;row=r;col=c; ;,稀疏矩阵元素的类TRiple,稀疏矩阵类的定义,#define MAXSIZE 500 template class SparseMatrix Triple dataMAXSIZE+1;int mu, nu, tu; /行数、列数、元素个数public:SparseMatrix( )mu=nu=tu=0;SparseMatrix(char*filename);SparseMatrix Transpose( );SparseMatrix Add(SparseMatrix b);Spar

16、seMatrix Muliply(SparseMatrix b);void print( )const; ;,template SparseMatrix:SparseMatrix(char *filename) ifstream fin; int i,r,c; T d;fin.open(filename, ios:in | ios:nocreate);if (!fin) cerr munutu;for (i = 1; i r cd; datai.SetVal(r,c,d);fin.close( ); ;,矩阵的转置 T=M,for(col=1;col=nu;col+)for(row=1;row

17、=mu;row+)Tcolrow=Mrowcol时间复杂度O(mu*nu),M row col elem1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,M row col elem2 1 12 3 1 9 1 3 36 3 143 4 242 5 181 6 154 6 -7,M row col elem1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,M row col elem1 3 -3 1 6 152 1 122 5 183 1 93 4 244 6 -76 3 14,稀疏矩阵的转置 T=

18、M 算法5.1,template SparseMatrix SparseMatrix : Transpose( ) SparseMatrix temp; int i, j, k=1;temp.mu=nu; temp.nu=mu; temp.tu=tu;if(tu)for(i=1;i=nu;+i)for(j=1;j=tu;+j) if(dataj.col=i)temp.datak.row=dataj.col;temp.datak.col=dataj.row; temp.datak.elem=dataj.elem; k+;return temp;,稀疏矩阵的转置的时间复杂度,O(nu*tu) 若t

19、unu*muO(nu*tu)=O(mu*nu*nu)不是一个好算法! 应当改进!,快速转置 依次逐个对号入座,M row col elem1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,M row col elem2 1 12 3 1 9,先求出M中每一列(即M中每一行)元素的个数numcol,确定M每行起始位置cpotcol。,快速转置,M row col elem1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,快速转置算法5.2,template SparseMatrix SparseMat

20、rix :Transpose( ) SparseMatrix temp; int i, j, k;int*num=new intnu+1; int* cpot=new intnu+1; cpot1=1; temp.mu=nu; temp.nu=mu;temp.tu=tu;if(tu)for(i=1;i=nu;+i)numi=0;for(i=1;i=tu;+i)+numdatai.col;for(i=1;inu;+i)cpoti+1=cpoti+numi; for(i=1;i=tu;+i)j=datai.col;k=cpotj;temp.datak.row=j; temp.datak.col=d

21、atai.row;temp.datak.elem=datai.elem; +cpotj; return temp; ,时间复杂性O(nu+tu),稀疏矩阵的加法,template SparseMatrix SparseMatrix: Add(SparseMatrix b ) SparseMatrix temp; if(mu!=b.mu|nu!=b.nu)cerr“error ”end;return temp;temp.mu=mu; temp.nu=nu;int i=1, j=1, k=1;while(i=tu|j=b.tu)if(datai.rowb.dataj.row) | (datai.r

22、ow=b.dataj.row),else if(datai.row=b.dataj.row) ,矩阵的乘法 Amp*Bpn,(aij)*(bij)=(1kpaik*bkj),2 0 0 -1 0 1 0 3 0 0 1 0,1 0 1 00 -1 0 00 2 0 13 0 1 0,a11*b11 a11*b12 a11*b13 a11*b14 a12*b21 a12*b22 a12*b23 a12*b24 a13*b31 a13*b32 a13*b33 a13*b34 a14*b41 a14*b42 a14*b43 a14*b44,时间复杂度mpn,稀疏矩阵的乘法,A row col ele

23、m1 2 12 1 3 9 3 1 33 6 144 3 245 2 186 1 156 4 -7,B row col elem rposrow1 1 8 rpos1= 12 3 4 rpos2= 23 2 1 rpos3=33 5 -1 4 1 2 rpos4=54 4 -35 1 5 rpos5=76 2 -2 rpos6=8,template SparseMatrix SparseMatrix: Multiply(SparseMatrix b ) SparseMatrix temp; if(nu!=b.mu) cer“error ”end;return temp;temp.mu=mu;t

24、emp.nu=b.nu;int *num=new intb.mu+1;int *rpos=new intb.mu+1; rpos1=1;T ctempb.nu+1;for(int i=1;i=b.mu;i+) numi=0;for(i=1;i=b.tu;i+)+numb.datai.row;for(i=1;i=b.mu;i+)rposi+1=rposi+numi;,int k=1,j, r,c,t=0;while(k=tu) for( j=1;j=b.nu;j+) ctempj=0;r=datak.row; while(k=tu,for(j=1;j=b.nu;j+)if(ctempj!=0)t

25、+; temp.datat.row=r;temp.datat.col=j;temp.datat.elem=ctempj;temp.tu=t;return temp; ,稀疏矩阵乘法的时间复杂度,O(tu.*b.nu),稀疏矩阵的输出,template void SparseMatrix : print( )const int i, j,k=1;for(i=1;i=mu;i+)for(j=1;j=nu;j+)if (i=datak.row) ,串,串-由字符组成的线性表,也叫 字符串 串是非数值计算算法处理的主要对象 在模式匹配,程序编译,数据处理等领域有广泛的应用,串类型的定义 串类型的实现

26、串的模式匹配,C语言中有关串的函数,#include strlen(char *s); strcpy(char *s, char *t); strcmp(char*s, char *t); strcat(char *s, char *t); strchr(char*s, char c); strrchr(char*s, char c);没有插入,删除 不好记,不好用,串类型的定义,1。用数组定义串#define MAXSTRLEN 255typedef unsigned SStringMAXSTRLEN+1;Sstring s1, s2;/用s10, s20存储各自的长度,2。堆分配存储表示,

27、typedef structchar *ch; int length;HString;HString s;s.length=15;s.ch=new chars.length;,3.块链存储,#define CHUNKSIZE 80 typedef struct Chunkchar chCHUNKSIZE;Chunk *next;; typedef structChunk *head,*tail;int curlen;LString; /定长字符串作元素的线性链表,堆分配存储串类的实现/“strclass.h”,#define STRING_CLASS #include #include #in

28、clude #ifndef NULL const int NULL = 0; #endif / NULL const int outOfMemory = 0, indexError = 1;,#ifndef STRING_CLASS,class String, char *str;int size;void Error(int errorType, int badIndex = 0) const;public:String(char *s = “ ” );String(const String,String,int operator (const String,int operator= (c

29、onst String,/ String functions,int Find(char c, int start) const;int FindLast(char c) const;String Substr(int index, int count) const;void Insert(const String,/ String I/Ofriend ostream,void String:Error(int errorType, int badIndex) const,if (errorType = outOfMemory)cerr “Memory exhausted!“ endl;els

30、ecerr “Index “ badIndex “ out of range“ endl;exit(1); ,/ constructor. allocate memory and copy in a C+String,String:String(char *s) size = strlen(s) + 1;str = new char size;/ terminate program if memory is exhausted.if (str = NULL)Error(outOfMemory);strcpy(str,s); ,/ copy constructor,String:String(c

31、onst String ,/ destructor,String:String(void) delete str; ,/ assignment operator. String to String,String ,/ assignment operator. C+String to String,String ,/ all relational operators use C+ string function strcmp,/ String = String int String:operator= (const String ,/ String = C+String,int String:o

32、perator= (char *s) const return strcmp(str,s) = 0; ,/ C+String = String. this is a friend function, since,/ the left operand is a C+String. int operator= (char *str, const String ,/ String != String,int String:operator!= (const String ,/ String != C+String,int String:operator!= (char *s) const retur

33、n strcmp(str,s) != 0; ,/ C+String != String,int operator!= (char *str, const String ,/ String String,int String:operator (const String ,/ String C+String,int String:operator (char *s) const return strcmp(str,s) 0; ,/ C+String String,int operator (char *str, const String ,/ String = String,int String

34、:operator= (const String ,/ String = C+String,int String:operator= (char *s) const return strcmp(str,s) = 0; ,/ C+String = String,int operator= (char *str, const String ,/ String String,int String:operator (const String ,/ String C+String,int String:operator (char *s) const return strcmp(str,s) 0; ,

35、/ C+String String,int operator (char *str, const String ,/ String = String,int String:operator= (const String ,/ String = C+String,int String:operator= (char *s) const return strcmp(str,s) = 0; ,/ C+String = String,int operator= (char *str, const String ,/ concatention: String + String,String String

36、:operator+ (const String / return temp ,/ concatention: String + C+String. same algorithm as,/ String + String, with a C+String as the right operand String String:operator+ (char *s) const String temp; int len; delete temp.str;len = size + strlen(s);temp.str = new char len;if (temp.str = NULL) Error

37、(outOfMemory);temp.size = len;strcpy(temp.str,str);strcat(temp.str, s);return temp; ,/ concatention: C+String + String. same algorithm as,/ String + String, with a C+String as the left operand String operator+ (char *cs, const String ,/ concatenate and assign: String += String,/ the current object i

38、s modified void String:operator+= (const String ,void String:operator+= (char *s), int len; char *tempstr;len = size + strlen(s);tempstr = new char len;if (tempstr = NULL) Error(outOfMemory);strcpy(tempstr,str);strcat(tempstr, s);delete str;str = tempstr;size = len; ,int String:Find(char c, int star

39、t) const, int ret;char *p;p = strchr( ,/ return index of last occurrence of c in string,int String:FindLast(char c) const int ret; char *p;/ use C+ library function strrchr. returns pointer to / the last occurrence of a character in the stringp = strrchr(str, c);if (p != NULL)ret = int(p-str); / com

40、pute indexelseret = -1; / return -1 on failurereturn ret; , int charsLeft = size-index-1, i;String temp; char *p, *q;if (index = size-1) return temp;if (count charsLeft) count = charsLeft;delete temp.str; temp.str = new char count+1;if (temp.str = NULL) Error(outOfMemory);for(i=0,p=temp.str,q= ,Stri

41、ng String:Substr(int index, int count) const,void String:Insert(const String& s, int index), int newsize, length_s = s.size-1, i;char *newstr, *p, *q;newsize = size + length_s;newstr = new char newsize;if (newstr = NULL)Error(outOfMemory);for(i=0,p = newstr, q = str; i = index-1;i+)*p+ = *q+;strcpy(

42、p,s.str); p += length_s;strcpy(p, ,void String:Insert(char *s, int index), int newsize, length_s = strlen(s), i;char *newstr, *p, *q;newsize = size + length_s;newstr = new char newsize;if (newstr = NULL) Error(outOfMemory);for(i=0,p = newstr, q = str;i = index-1;i+)*p+ = *q+;strcpy(p,s); p += length

43、_s; strcpy(p, ,void String:Remove(int index, int count), int charsLeft = size-index-1, newsize, i;char *newstr, *p, *q;if (index = size-1) return; if (count charsLeft) count = charsLeft;newsize = size - count;newstr = new char newsize;if (newstr = NULL)Error(outOfMemory);for(i=0,p=newstr,q=str;i = i

44、ndex-1;i+)*p+ = *q+;q += count; strcpy(p,q); delete str;size = newsize; str = newstr; ,/ String index operator,char ,/ pointer conversion operator,String:operator char* (void) const return str; ,istream& operator (istream& istr, String& s),char tmp256;if (istr tmp) delete s.str; / delete existing st

45、rings.size = strlen(tmp) + 1;s.str = new char s.size;if (s.str = NULL) s.Error(outOfMemory);strcpy(s.str,tmp);return istr; ,ostream& operator (ostream& ostr, const String& s),ostr s.str;return ostr; ,/ read characters from istr,int String:ReadString (istream / return -1 on end of file ,int String:Length(void) const,return size-1; ,int String:IsEmpty(void) const,return size = 1; ,void String:Clear(void),

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

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

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报