数据结构----------稀疏矩阵的基本运算
漂亮的代码片
#include< iostream>
using namespace std;
#define M 4
#define N 4
#define MaxSize 16
#define ElemType int
typedef struct{
int r;
int c;
ElemType d;
} TupNode;
typedef struct{
int rows;
int cols;
int nums;
TupNode data[ MaxSize] ;
} TSMatrix;
void CreatMat ( TSMatrix& t, ElemType A [ M ] [ N ] ) {
int i, j;
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 ) {
t. data[ t. nums] . r= i;
t. data[ t. nums] . c= j;
t. data[ t. nums] . d= A [ i] [ j] ;
t. nums++ ;
}
}
}
}
void DispMat ( TSMatrix t) {
if ( t. nums<= 0 ) ;
else {
cout<< "rows:" << t. rows<< "\t" << "cols:" << t. cols<< "\t" << "nums:" << t. nums<< endl;
cout<< "row" << "\t" << "col" << "\t" << "data" << endl;
for ( int k= 0 ; k< t. nums; k++ )
cout<< t. data[ k] . r<< "\t" << t. data[ k] . c<< "\t" << t. data[ k] . d<< endl;
}
}
void TranMat ( TSMatrix t, TSMatrix & tb) {
int k, kl= 0 , v;
tb. rows= t. cols;
tb. cols= t. rows;
tb. nums= t. nums;
if ( t. nums!= 0 ) {
for ( v= 0 ; v< t. cols; v++ ) {
for ( k= 0 ; k< t. nums; k++ )
if ( t. data[ k] . c== v) {
tb. data[ kl] . r= t. data[ k] . c;
tb. data[ kl] . c= t. data[ k] . r;
tb. data[ kl] . d= t. data[ k] . d;
kl++ ;
}
}
}
}
void Swap ( TupNode& a, TupNode& b) {
int m= a. r;
int n= a. c;
ElemType q= a. d;
a. r= b. r;
a. c= b. c;
a. d= b. d;
b. r= m;
b. c= n;
b. d= q;
}
void MatAdd ( TSMatrix a, TSMatrix b, TSMatrix& c) {
if ( ( a. rows!= b. rows) || ( a. cols!= b. cols) ) {
cout<< "两个矩阵的行列数不相等" << endl;
}
else { c. rows= a. rows;
c. cols= b. cols;
c. nums= 0 ;
for ( int k= 0 ; k< a. nums; k++ ) {
c. data[ c. nums] . r= a. data[ k] . r;
c. data[ c. nums] . c= a. data[ k] . c;
c. data[ c. nums] . d= a. data[ k] . d;
c. nums++ ;
}
int flag= 0 ;
for ( int i= 0 ; i< b. nums; i++ ) {
for ( int j= 0 ; j< a. nums; j++ ) {
if ( b. data[ i] . r== a. data[ j] . r&& b. data[ i] . c== a. data[ j] . c) {
c. data[ j] . d+= b. data[ i] . d;
flag= 1 ;
break ;
}
}
if ( flag== 0 ) {
c. data[ c. nums] . r= b. data[ i] . r;
c. data[ c. nums] . c= b. data[ i] . c;
c. data[ c. nums] . d= b. data[ i] . d;
c. nums++ ;
}
flag= 0 ;
}
for ( int i= 0 ; i< c. nums- 1 ; i++ )
for ( int j= 0 ; j< c. nums- 1 - i; j++ )
if ( c. data[ j] . r> c. data[ j+ 1 ] . r|| ( c. data[ j] . r== c. data[ j+ 1 ] . r&& c. data[ j] . c> c. data[ j+ 1 ] . c) )
Swap ( c. data[ j] , c. data[ j+ 1 ] ) ;
}
}
int getvalue ( TSMatrix t, int i, int j) {
for ( int m= 0 ; m< t. nums; m++ ) {
if ( t. data[ m] . r== i&& t. data[ m] . c== j) {
return t. data[ m] . d;
}
}
return 0 ;
}
void MutMul ( TSMatrix a, TSMatrix b, TSMatrix& c) {
if ( a. cols!= b. rows) {
cout<< "前一矩阵列数与后一矩阵行数不相等" << endl;
}
else {
int d[ a. rows] [ 4 ] ;
for ( int i = 0 ; i < a. rows ; i++ ) {
for ( int j = 0 ; j < b. cols; j++ ) {
d[ i] [ j] = 0 ;
}
}
for ( int i = 0 ; i < a. rows ; i++ ) {
for ( int j = 0 ; j < b. cols; j++ ) {
for ( int k = 0 ; k < a. cols; k++ ) {
d[ i] [ j] += getvalue ( a, i, k) * getvalue ( b, k, j) ;
}
}
}
CreatMat ( c, d) ;
}
}
int main ( ) {
int a[ 4 ] [ 4 ] = { { 1 , 0 , 3 , 0 } ,
{ 0 , 1 , 0 , 0 } ,
{ 0 , 0 , 1 , 0 } ,
{ 0 , 0 , 1 , 1 } } ;
int b[ 4 ] [ 4 ] = { 3 , 0 , 0 , 0 ,
0 , 4 , 0 , 0 ,
0 , 0 , 1 , 0 ,
0 , 0 , 0 , 2 } ;
TSMatrix aa;
TSMatrix bb;
TSMatrix ta;
TSMatrix c;
TSMatrix ab;
CreatMat ( aa, a) ;
CreatMat ( bb, b) ;
cout<< endl<< endl;
cout<< "稀疏矩阵a:" << endl;
DispMat ( aa) ;
cout<< endl<< endl;
cout<< "稀疏矩阵b:" << endl;
DispMat ( bb) ;
cout<< endl<< endl;
cout<< "a转置矩阵:" << endl;
TranMat ( aa, ta) ;
DispMat ( ta) ;
cout<< endl<< endl;
cout<< "a+b:" << endl;
MatAdd ( aa, bb, c) ;
DispMat ( c) ;
cout<< endl<< endl;
cout<< "axb:" << endl;
MutMul ( aa, bb, ab) ;
DispMat ( ab) ;
}