Str AssignT chars Str CopyT S Str EmptyS

  • Slides: 26
Download presentation

Str. Assign(&T, chars) Str. Copy(&T, S) Str. Empty(S) Str. Compare(S, T) Str. Length(S) Clear.

Str. Assign(&T, chars) Str. Copy(&T, S) Str. Empty(S) Str. Compare(S, T) Str. Length(S) Clear. String(&S) Concat(&T, S 1, S 2) Substring(&Sub, S, Pos, len) Index(S,T,pos) Replace(&S, T, V) Str. Insert(&S, pos, T) Str. Delete(&S, pos, len) Destroy. String(&S) strcpy strlen(S)==0 strcmp strlen strcat 0<=pos<=Strlength(S)-1 strstr 0<=pos<=Strlength(S) 0<=pos<=Str. Length(S)-len } ADT String 最小操作子集 Str. Assign、Str. Compare、Str. Length、Concat,Substring ypb@ustc. edu. cn 3 中国科学技术大学

4. 2串的表示和实现 4. 2. 1定长顺序存储表示 • 两种表示方法 – 下标为 0的数组单元存放长度 (pascal) typedef unsigned char

4. 2串的表示和实现 4. 2. 1定长顺序存储表示 • 两种表示方法 – 下标为 0的数组单元存放长度 (pascal) typedef unsigned char SString[MAXSTLEN+1] ; – 在串值后面加‘’结束 (C语言) char str[10]; 算法 Status Concat(Sstring &T, Sstring S 1, Sstring S 2) 【注意】 T[]是否足够长度,溢出处理 算法 void Substring(char Sub[], char S[], int pos, int len) …Strncpy(Sub, &S[pos], len); Sub[len]=‘’; … ypb@ustc. edu. cn 4 中国科学技术大学

Ø 获得next数组的算法 void get_next(Sstring T, int &next[]){ i=1; next[1]=0; j=0; while(i<T[0]){ if(j==0 || T[i]==T[j]){++i;

Ø 获得next数组的算法 void get_next(Sstring T, int &next[]){ i=1; next[1]=0; j=0; while(i<T[0]){ if(j==0 || T[i]==T[j]){++i; ++j; next[i]=j; } else j=next[j]; } } Ø 改进后获得next数组(nextval)算法 (红色部分改写如下) if(T[i]!=T[j])nextval[i]=j; else nextval[i]=nextval[j]; ypb@ustc. edu. cn 8 中国科学技术大学

4. 3数组 数组的定义 ADT Array{ 数据对象:D={aj 1 aj 2…ajn| aj 1 aj 2…ajn∈Elemset, ji=0,

4. 3数组 数组的定义 ADT Array{ 数据对象:D={aj 1 aj 2…ajn| aj 1 aj 2…ajn∈Elemset, ji=0, 1, …bi-1, bi是数组第i维的长度,n是位数} 数据关系:R={R 1, R 2…Rn} Ri={< aj 1…aji…ajn , aj 1…aji+1…ajn >| aj 1…aji…ajn , aj 1…aji+1…ajn ∈D, i=2, 3…n 0<=jk<=bk-1, 1<=k<=n, k!=I 0<=ji<=bi-1 } ypb@ustc. edu. cn 9 中国科学技术大学

基本操作 : init. Array(&A, n, bound 1, bound 2. . . boundn) Destroy. Array(&A)

基本操作 : init. Array(&A, n, bound 1, bound 2. . . boundn) Destroy. Array(&A) Value(A, &e, index 1, index 2. . . indexn) Assign(&A, e, index 1, index 2. . . indexn) }ADT Array • 二维数组定义 – 其数据元素是一维数组的线形表 • N维数组定义 – 其数据元素是N-1维数组的线形表 ypb@ustc. edu. cn 10 中国科学技术大学

特征值 i-j=0 Mat[0, 0] s g a b a c b a d f

特征值 i-j=0 Mat[0, 0] s g a b a c b a d f g b a c s t g 1 a 1 1 b 1 1 a 1 1 1 a b i-j 为固定值 1 1 f g 1 1 d 特征值 i-j=m-1 特征值 i-j=-(n-1) 1 1 1 Mat[m-1, n-1] 1 1

随机稀疏矩阵的存储表示 • 三元组顺序表 const MAXSIZE=1000 typedef struct{ int i, j; Element. Type e; }Triple;

随机稀疏矩阵的存储表示 • 三元组顺序表 const MAXSIZE=1000 typedef struct{ int i, j; Element. Type e; }Triple; typedef struct{ Triple data[MAXSIZE+1]; int mu, nu, tu; }TSMatrix; ypb@ustc. edu. cn 15 中国科学技术大学

三元组顺序表示例 • (下标从1开始) (a) M. data i j e 1 1 3 -3 9

三元组顺序表示例 • (下标从1开始) (a) M. data i j e 1 1 3 -3 9 2 1 6 15 1 -3 3 2 1 12 3 6 14 4 2 5 18 5 4 3 24 5 3 1 9 6 5 2 18 6 3 4 24 7 6 1 15 7 4 6 -7 8 6 4 -7 8 6 3 14 i j e 1 1 2 12 2 1 3 3 3 4 (b) T. data cpot[1]=1 cpot[col]=cpot[col-1]+num[col-1] 2<=col<=nu col 1 2 3 4 5 6 7 num[col] 2 2 2 1 0 cpot[col] 1 3 5 7 8 8 9

 • 矩阵转置 void transpose(Elem. Type M[][], T[][]) 时间复杂度O(n×m) • “按需点菜”的思想 复杂度O(M. nu×M. tu)

• 矩阵转置 void transpose(Elem. Type M[][], T[][]) 时间复杂度O(n×m) • “按需点菜”的思想 复杂度O(M. nu×M. tu) • “按位就座”的思想 – 建立辅助数组 num[n] cpot[n] void createpos(TSMatrix M) – 快速转置 status fast. Transpose. SMatrix(TSMatrix M, TSMatrix T) 时间复杂度 O(M. nu+M. tu) ypb@ustc. edu. cn 17 中国科学技术大学

 • 十字链表 – 矩阵运算增加或减少非零元时,使用链表结构来表 示三元组序列。 typedef struct OLNode{ int i, j; Element. Type

• 十字链表 – 矩阵运算增加或减少非零元时,使用链表结构来表 示三元组序列。 typedef struct OLNode{ int i, j; Element. Type e; struct OLNode *rnext, *cnext; }OLNode, *Olink; typedef struct { Olink *rhead, *chead; int mu, nu, tu; }Cross. List; ypb@ustc. edu. cn 19 中国科学技术大学

Status Concat(SString &T, SString S 1, SString S 2) { if (S 1[0]+S 2[0]

Status Concat(SString &T, SString S 1, SString S 2) { if (S 1[0]+S 2[0] <= MAXSTRLEN) { // 未截断 for (i=1; i<=S 1[0]; i++) T[i] = S 1[i]; for (i=1; i<=S 2[0]; i++) T[i+S 1[0]] = S 2[i]; T[0] = S 1[0]+S 2[0]; uncut = TRUE; } else if (S 1[0] < MAXSTRLEN) { // 截断 for (i=1; i<=S 1[0]; i++) T[i] = S 1[i]; for (i=S 1[0]+1; i<=MAXSTRLEN; i++) T[i] = S 2[i-S 1[0]]; T[0] = MAXSTRLEN; uncut = FALSE; } else { // 截断(仅取S 1) for (i=0; i<=MAXSTRLEN; i++) T[i] = S 1[i]; uncut = FALSE; } return uncut; } // Concat ypb@ustc. edu. cn 21 中国科学技术大学

Status Str. Insert(HString &S, int pos, HString T) { // 1≤pos≤Str. Length(S)+1。在串S的第pos个字符之前插入串T。 if (pos

Status Str. Insert(HString &S, int pos, HString T) { // 1≤pos≤Str. Length(S)+1。在串S的第pos个字符之前插入串T。 if (pos < 1 || pos > S. length+1) return ERROR; if (T. length) { // T非空, 则重新分配空间, 插入T if (!(S. ch = (char *)realloc(S. ch, (S. length+T. length) *sizeof(char)))) return ERROR; for (i=S. length-1; i>=pos-1; --i) // 为插入T而腾出位置 S. ch[i+T. length] = S. ch[i]; for (i=0; i<T. length; i++) // 插入T S. ch[pos-1+i] = T. ch[i]; S. length += T. length; } return OK; } // Str. Insert ypb@ustc. edu. cn 22 中国科学技术大学

三元组存储数组的快速转置 Status Fast. Transpose. SMatrix(TSMatrix M, TSMatrix &T) { // 算法 5. 2 //

三元组存储数组的快速转置 Status Fast. Transpose. SMatrix(TSMatrix M, TSMatrix &T) { // 算法 5. 2 // 采用三元组顺序表存储表示,求稀疏矩阵M的转置矩阵T T. mu = M. nu; T. nu = M. mu; T. tu = M. tu; if (! T. tu) return FAIL; for (col=1; col<=M. nu; ++col) num[col] = 0; for (t=1; t<=M. tu; ++t) // 求 M 中每一列所含非零元的个数 ++num[M. data[t]. j]; cpot[1] = 1; // 求 M 中每一列的第一个非零元在 b. data 中的序号 for (col=2; col<=M. nu; ++col) cpot[col] = cpot[col-1]+num[col-1]; for (p=1; p<=M. tu; ++p) { col = M. data[p]. j; q = cpot[col]; T. data[q]. i =M. data[p]. j; T. data[q]. j =M. data[p]. i; T. data[q]. e =M. data[p]. e; ++cpot[col]; } // for } // Fast. Transpose. SMatrix ypb@ustc. edu. cn 26 中国科学技术大学