-
三元组表的C++语言描述
-
基本运算的算法——建立稀疏矩阵的三元组表的算法、按矩阵的列序转置算法、按矩阵的行序转置算法
#include<iostream> using namespace std; template <class T> class sanyuanzu { private: struct Node { int i; int j; T data; Node(int x,int y,T a){ i = x; j = y; data = a; } }; int length = 0; int maxSize; int Rows;//矩阵行数 int Cols;//矩阵列数 int number;//非0个数 Node * dusk; public: sanyuanzu(int Max = 10) { cout<<"Rows:"<<endl; cout<<"Cols:"<<endl; cout<<"Number:"<<endl; cin>>Rows>>Cols>>number; maxSize = Max; dusk[maxSize]; length = 0; } void insertdata(int x,int y,T a) { Node tem(x,y,a); dusk[length] = tem; length ++; } void transpose(sanyuanzu & a) { int q = 0; for(int col = 1;col <= Cols;col++) { for(int p = 0;p < number;p++ ) { if(dusk[p].j==col) { a.dusk[q].i = dusk[p].j; a.dusk[q].j = dusk[p].i; a.dusk[q].data = dusk[p].data; q++; } } } } int sanyuanzu_number() { return number; } void print() { cout<<endl; for(int i = 0 ; i < number ; i++) { cout<<dusk[i].i<<" | "<<dusk[i].j<<" | "<<dusk[i].data<<endl; } } }; int main() { cout<<"duskl1"<<endl; sanyuanzu<int> duskl1; cout<<"duskl2"<<endl; sanyuanzu<int> duskl2; for(int i = 0 ; i < duskl1.sanyuanzu_number() ; i++) { int x,y,z; cin>>x>>y>>z; duskl1.insertdata(x,y,z); } duskl1.transpose(duskl2); duskl2.print(); }