Array one-dimensional arrays
The storage address of the array element labeled I (0≤i<n) arrname[i] Loc (Arrname[i]) is
LOC (Arrname[i]) =loc (arrname[0]) +i*sizeof (elemtype) (0≤i<n) loc (arrname[0]) is called the base site
Two-dimensional arrays
The storage address of the array element Arrname[i][j] Loc (Arrname[i][j]) is
LOC (Arrname[i][j]) =loc (arrname[0][0]) + (i*n+j) sizeof (Elemtype) (0≤i<m,0≤j<n) loc (arrname[0][0]) is called the base site
Abstract data types for multidimensional array arrays
Three-dimensional integer array
#include <stdio.h>#defineERROR 0#defineOK 1#defineNotpresent 2#defineDuplicate 3#defineIllegalindex 4typedefintStatus;typedefstructtriplearray{intM1; intm2; intm3; int*Array;} Triplearray; Status Createarray (Triplearray*triplearray,intM1,intM2,intm3) {Triplearray->m1=M1; Triplearray->m2=m2; Triplearray->m3=m3; Triplearray->array= (int*)malloc(m1*m2*m3*sizeof(int)); if(! Triplearray->array)
returnERROR; returnOK;} Status Destroyarray (Triplearray*Triplearray) { if(! Triplearray)returnERROR; if(Triplearray->array) Free(triplearray->array); Free(Triplearray); returnOK;} Status Retrievearray (Triplearray Triplearray,intI1,intI2,inti3,int*x) {//data element Query operations if(! Triplearray.array)returnnotpresent; if(i1<0|| i2<0|| i3<0|| i1>=triplearray.m1| | i2>=triplearray.m2| | i3>=triplearray.m3)returnIllegalindex; *x=* (triplearray.array+i1*m2*m3+i2*m3+i3); returnOK;} Status Storearrayitem (Triplearray*triplearray,intI1,intI2,inti3,intx) { if(! Triplearray->array)returnnotpresent; if(i1<0|| i2<0|| i3<0|| i1>=triplearray->m1| | i2>=triplearray->m2| | I3>=triplearray->m3)returnIllegalindex; * (TRIPLEARRAY->ARRAY+I1*M2*M3+I2*M3+I3) =x; returnOK;}voidOutputarray (Triplearray triplearray) {intI1,i2,i3; if(! Triplearray.array)returnERROR; for(i1=0; i1<triplearray.m1;i1++) { for(i2=0; i2<triplearray.m2;i2++) { for(i3=0; i3<triplrarray.m3;i3++) { intvalue; Retrievearray (Triplearray,i1,i2,i3,&value); printf ("array[%d][%d][%d]=%d\n", I1,i2,i3,value); } } } returnOK;} Status Copyarray (Triplearray*triplearraya,triplearray *Triplearrayb) { if(! triplearraya->array| |! Triplearrayb->array)returnnotpresent; if(Triplearraya->array==triplearrayb->array)returnDuplicate; if(triplearraya->m1!=triplearrayb->m1| | triplearraya->m2!=triplearrayb->m2| | TRIPLEARRAYA->M3!=TRIPLEARRAYB->M3)returnERROR; intI1,i2,i3; for(i1=0; i1<triplearraya->m1;i1++) for(i2=0; i2<triplearraya->m2;m2++) for(i3=0; i3<triplearraya->m3;m3++) { intvalue; Retrievearray (Triplearraya,i1,i2,i3,&value); Storearrayitem (Triplearrayb,i1,i2,i3,value); } returnOK;}voidMainvoid){ intI1,i2,i3; Triplearray Triplearraya,triplearrayb; Createarray (&triplearraya,2,2,2); Createarray (&triplearrayb,2,2,2); for(i1=0; i1<triplearraya.m1;i1++) for(i2=0; i2<triplearraya.m2;m2++) for(i3=0; i3<triplearraya.m3;m3++) {Retrievearray (Triplearraya,i1,i2,i3,Ten); Storearrayitem (Triplearrayb,i1,i2,i3,5); } outputarray (Triplearraya); Outputarray (TRIPLEARRAYB); Copyarray (&TripleArrayA,&Triplearrayb); Outputarray (Triplearraya); Outputarray (Triplearrayb);}Special matrix symmetric matrices
Features Aij=aji
When storing, you need to contract a storage rule: Row first or column first
① Stores the following elements of the main diagonal (including the diagonal) in a row-by-priority order in order to hold a vector sa[0... n (n+1)/2-1] (in the lower triangular matrix, the total number of elements is n (n+1)/2). of which: sa[0]=A0,0sa[1]=A1,0... sa[n (n+1)/2-1]=an-1, N-1② element AIJ storage position AIJ element I row before (from line No. 0 to I-1 lines), altogether there are:1+2+...+i=ix (i+1)/2 elements. On line I, preceded by a J element, namely Ai0,ai1,..., ai,j-1, so there are: Sa[ix (i+1)/2+j]=the correspondence between Aij③aij and Sa[k]: if I≥j,k=ix (i+1)/2+j0≤k<n (n+1)/2If I<J,K=JX (j+1)/2+i0≤k<n (n+1)/2Order I=max (i,j), j=min (i,j), the correspondence between K and I,j can be unified as: K=ix (i+1)/2+j0≤k<n (n+1)/2(3) address calculation formula for symmetric matrices Loc (AIJ)=LOC (sa[k])=loc (sa[0]) +kxd=loc (sa[0]) +[ix (i+1)/2+J]xd by the Subscript transformation formula, you can immediately find the matrix element AIJ in its compressed storage represents the corresponding position K in the SA. Therefore, it is a random access structure. "Example" A21 and A12 are stored in sa[4], this is because K=ix (i+1)/2+j=2X (2+1)/2+1=4
Triangular matrix
Upper triangular matrix and lower triangular matrix
Refer to the blog: 78134780?locationnum=5&fps=1
Sparse matrices
Using ternary group <i,j,aij>, divided into rows of triples and columns of three-tuple table, respectively, according to the line number and column number from small to large order.
Data structure _4 arrays and strings