In this paper, the basic series of data Structure network course (5): Array and generalized table in the 3rd Class sparse matrix ternary representation.
The ternary group of sparse matrices represents the related algorithm library using the program's multi-file organization, including two files:
1. header file: Tup.h, contains the code that defines a ternary group of sparse matrices representing the data structure, the macro definition, the declaration of the function to implement the algorithm;
#ifndef tup_h_included#define Tup_h_included#define M 6#define N 7#define MAXSIZE 100//matrix non-0 elements maximum numbertypedefintElemtype;typedefstruct{intR//Line number intC//Column numberElemtype D;//Element value} Tupnode;//ternary group definitiontypedefstruct{intRows//number of rows intCols//Number of columns intNums//Non 0 element countTupnode data[maxsize];} Tsmatrix;//Ternary order table definitionvoidCreatmat (Tsmatrix &t,elemtype a[m][n]);//create its ternary representation from a two-dimensional sparse matrixBOOLValue (Tsmatrix &t,elemtype x,intIintj);//Ternary element assignmentBOOLAssign (Tsmatrix t,elemtype &x,intIintj);//assigns the element value of the specified position to the variablevoidDispmat (Tsmatrix t);//Output ternary groupvoidTrantat (Tsmatrix T,tsmatrix &TB);//Matrix transpose#endif//tup_h_included
2. source file: Tup.cpp, which contains definitions of functions that implement various algorithms
#include "stdio.h"#include "tup.h"void Creatmat (Tsmatrix &t,elemtype a[m][n])//create its ternary representation {int i,j from a two-dimensional sparse matrix;T. Rows=m;T. cols=n;T. Nums=0;for (i=0; i<m; i++){for (j=0; j<n; j + +)if (a[i][j]!=0)//Store only non-0 elements {T. Data[t. Nums]. R=i;T. Data[t. Nums]. C=j;T. Data[t. Nums]. D=A[I][J];T. Nums++;}}}bool Value (Tsmatrix &t,elemtypex, int i,int j)//Ternary element assignment {int k=0, K1;if (i>=t. Rows|| J>=t. cols) return False; return false On Failurewhile (k<t. Nums&& i>t. DataK. R) k++; Find Rowswhile (k<t. Nums&& i==t. DataK. R&& j>t. DataK. C) k++;//Lookup Columnif (t. DataK. R==i && T. DataK. C==J)//existence of such elements t. DataK. D=x;else//does not exist when such an element is inserted into an element {for (k1=t. Nums-1; k1>=k; k1--){T. Data[k1+1]. R=t. Data[K1]. R;T. Data[k1+1]. C=t. Data[K1]. C;T. Data[k1+1]. D=t. Data[K1]. D;} t. DataK. R=i;T. DataK. C=j;T. DataK. D=x;T. Nums++;} return True; Return true on Success}bool Assign (Tsmatrix T,elemtype &x, int i,int j)//assigns the element value of the specified position to the variable {int k=0;if (i>=t. Rows|| J>=t. cols) return False; return false On Failurewhile (k<t. Nums&& i>t. DataK. R) k++; Find Rowswhile (k<t. Nums&& i==t. DataK. R&& j>t. DataK. C) k++;//Lookup Columnif (t. DataK. R==i && T. DataK. C==J)x=t. DataK. D;Elsex=0; No representation is found in triples 0 elementsreturn True; Return true on Success}void Dispmat (Tsmatrix t)//output ternary group {int I;if (t. Nums<=0)//Returns return if no 0 elements are returned;printf"\t%d\t%d\t%d\n"T. RowsT. colsT. Nums);printf"\ t------------------\ n");for (i=0; i<t.nums; i++)printf"\t%d\t%d\t%d\n"T. DataI. RT. DataI. CT. DataI. D);}void Trantat (Tsmatrix t,tsmatrix &TB)//matrix transpose {int p,q=0V; Q is the subscript of the Tb.dataTb. Rows=t. cols;Tb. cols=t. Rows;Tb. Nums=t. Nums;if (t. Nums!=0)//When a non-0 element is present, perform a transpose {for (v=0, v<t.cols; v++)//tb.data[q] in the order of C fieldsfor (p=0; p<t.nums; p++)//p for T.data subscriptif (t. Data[P]. C==V) {TB. Data[Q]. R=t. Data[P]. C;Tb. Data[Q]. C=t. Data[P]. R;Tb. Data[Q]. D=t. Data[P]. D;q++;} }}
3. Create a source file (such as main.cpp) in the same project, and compile the main function to complete the relevant testing work. Cases:
#include <stdio.h>#include "tup.h"int main () {Tsmatrix t,tb; int x,y=Ten; int a[6][7]= { {0,0,1,0,0,0,0}, {0,2,0,0,0,0,0}, {3,0,0,0,0,0,0}, {0,0,0,5,0,0,0}, {0,0,0,0,6,0,0}, {0,0,0,0,0,7,4} }; Creatmat (T,a); printf"b:\n"); Dispmat (t);if(Assign (T,x,2,5)==true)//Returned when calledtruePrintf("Assign (t,x,2,5) =>x=%d\n", x);ElseReturned when calledfalse printf("Assign (t,x,2,5) = parameter error \ n");Value(T,y,2,5);printf("Execute value (t,10,2,5) \ n");if (Assign (t,x,2,5) = =true)Returned when calledtrue printf("Assign (t,x,2,5) =>x=%d\n", x);ElseReturned when calledfalse printf("Assign (t,x,2,5) = parameter error \ n");printf("b:\n");Dispmat(t);Trantat(T,TB);printf("Matrix transpose tb:\n");Dispmat(TB);return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Self-built algorithm library of data structure--ternary representation of sparse matrices