1、Chapter 4,ARRAYS AND MATRICES,4.1 ARRAYS,数组是一种常用(熟悉)的数据结构。 线性表包括:数组和广义表。4.4.1 The Abstract Data TypeEach instance of the data object array is a set of pairs of the form (index, value). ExampleHigh = (0,82), (1,79), (2,85), (3,92), (4,88), (5, 89), (6,91) The functions performed:Create Creates an ini
2、tially empty arrayStore Adds a pair of the form to the set.Retrieve Retrieves the value of the pair that has a given index.,AbstractDataType Array instances set of (index, value) pairs, no two pairs have the same indexoperations Create ( ): create an empty array Store (index,value ): add this pair t
3、o set deleting existing pair (if any) with the same index Retrieve (index ): return the pair with this index value ADT 4.1 Abstract data type specification of an array,4.1.2 Indexing a C+ Array,The index (also called subscript) of an array in C+ must be of the form i1i2i3 ik int score u1u2u3 uk(数组需要
4、预留空间)The memory begins at byte start and extends up to and including byte start+size(score)-1,4.1.3 Row- and Column-Major Mappings,数组的存储方式row major mapping (按行排列)column major mapping (按列排列)访问元素位置: Start+map( i1,i2, i3, ik )*sizeof(int),00 01 02 03 04 0510 11 12 13 14 1520 21 22 23 24 25Figure 4.1 Ta
5、bular arrangement of indices for int score360 1 2 3 4 5 0 3 6 9 12 156 7 8 9 10 11 1 4 7 10 13 1612 13 14 15 16 17 2 5 8 11 14 17Row-major mapping (b) Column-major mappingFigure 4.2 Mapping a two-dimensional array,4.1.4 the Class Array1D,一维数组可以看成是一个线性表或向量,适合于随机查找。 Although C+ support one dimensional
6、 arrays,this support is rather primitive.Ex.: To declared an array int a 9 Accessing a-3, a90 are invalid indexes.the array cannot be output using the statement cout a endl;,template class Array1D public : Array1D(int size = 0); Array1D(const Array1DProgram 4.1 A one-dimensional array class,template
7、 Array1D:Array1D(int sz) / Constructor for one-dimensional arrays. if (sz Array1D:Array1D(const Array1D Program 4.2 Constructors for a one-dimensional array,template T Program 4.4 Overloading the assignment operator,template Array1D Array1D:operator - (const Array1D Program 4.5 Overloading binary mi
8、nus, unary minus, and increment,4.1.5 The Class Array2D,A two-dimensional array is viewed as a collection of one-dimensional array.怎样将二维数组中元素存入到计算机内存中呢?数组一般不作插入或删除操作,一旦建立,则结构中的元素个数和相互间的关系就不再变动,即它们的逻辑结构不再发生变化。,template class Array2D public: Array2D (int r = 0, int c = 0); Array2D(const Array2DProgram
9、 4.6 A class for two-dimensional arrays,template Array2D:Array2D(int r, int c) if (r r; for (int i = 0; i Array2D:Array2D(const Array2D Program 4.9 Overloading for a two-dimensional array,template Array2D Array2D:operator - (const Array2D Program 4.11 Matrix multiplication,4.2 MATRICES 4.2.1 Definit
10、ions and Operations,MT(i,j) = M(j,i), 1in, 1j n) throw OutOfBounds(); switch (i - j) case 1: return ti - 2;case 0: return tn + i - 2;case -1: return t2 * n + i - 2;default: return 0; Program 4.18 The class TridiagonalMatrix,4.3.4 Triangular Matrices,Program 4.19 The class LowerMatrixtemplate class L
11、owerMatrix public : LowerMatrix(int size = 10) n = size; t = new Tn*(n+1)/2; LowerMatrix( ) delete t; LowerMatrix,template LowerMatrix ,4.3.5 Symmetric Matrices,4.4 Sparse Matrices(稀疏矩阵) 4.4.1 Motivation,什么是稀疏矩阵?矩阵的许多元素为0如何进行压缩存储?三元组表示(i, j, aij)An m x n matrix is said to be sparse if many of its el
12、ements are zero. A matrix that is not sparse is dense.,4.4.2 Array Representation,template class SparseMatrix friend ostreamProgram 4.20 The class SparseMatrix,Program 4.21 SparseMatrix constructortemplate SparseMatrix:SparseMatrix(int maxTerms) if (maxTerms MaxTerms; terms = rows = cols = 0;,templa
13、te ostream Program 4.22 SparseMatrix input and output Codes,template /转置结果在b中void SparseMatrix:Transpose(SparseMatrix Program 4.23 Transposing a sparse matrix,Program 4.24 Appending a non-zero termtemplate void SparseMatrix:Append(const Term ,Program 4.25 Adding two sparse matricestemplate / C =(*th
14、is) + bvoid SparseMatrix:Add(const SparseMatrix ,else if (indt = indb) if (act.value + b.acb.value) term t; t.row = act.row;t.col=act.col; t.value=act.value+b.acb.value; c.append(t); ct+; cb+;else c.append(b.acb); cb+; for (; ctterms; ct+) c.append (act); for (; cb,0 0 0 2 0 0 1 00 6 0 0 7 0 0 30 0
15、0 9 0 8 0 00 4 5 0 0 0 0 0,template class CNode public: int operator != (const CNode ,template class HeadNode public: int operator !=(const HeadNode Program 4.26 Chain nodes used in sparse matrix representation,3. The Class LinkedMatrixtemplate class LinkedMatrix public: LinkedMatrix( ) ; LinkedMatr
16、ix( ) ; void Transpose(LinkedMatrix Program 4.27 Class definition for a linked sparse matrix,4. Overloading templateistream,H.row = row; H.a.Zero(); / add new term to row chain CNode *c = new CNode; c-col = col; c-value = value; H.a.Append(*c); / take care of last row of matrix if (H.row) x.a.Append
17、(H); H.a.Zero(); / save from chain destructor return in; Program 4.28 Input a sparse matrix,5. Overloading ostreamProgram 4.29 Output a sparse matrix,6. The function Transpose(转置一个稀疏矩阵)templatevoid LinkedMatrix:Transpose(LinkedMatrix / go to next row,b.rows = cols; b.cols = rows;HeadNode H;for (int i = 1; i = cols; i+) if (!bini.IsEmpty( ) / 转置第i行 H.row = i; H.a = bini; b.a.Append(H); bini.Zero(); H.a.Zero(); / save from destructordelete bin;Program 4.30 Transpose a sparse matrix,Chapter 4ex.27, ex.29, ex36, ex38,EXERCISES,