c++数据结构 稀疏矩阵转置实验

2024年11月22日 07:54
有1个网友回答
网友(1):

struct elm3 {
int row,col;
double d;
elm3():row(0),col(0),d(0){};
elm3(int i,int j,double data):row(i),col(j),d(data){};
};
template list parseMat;
parseMat m;

elem3 e;
int a[][4]={{2,0,0,1},{0,2,0,0},{1,0,3,0},{4,2,0,2}};
m.push_back(elm3(m,n,a[i][j]);

for(i=0;i for(j=0;j{
if(a[i][j]) m.push_back(elm3(i,j,a[i][j]);
};

paraseMat &SortMat(parseMat & x )
{
elm3 e=*x.begin();
x.erase(x.begin());
sort(x.begin(),x.end());
x.insert(x.begin(),e);
}
paraseMat Trans(const parseMat & x )
{
parseMat y;
parseMat::interator iter =x.begin() ;
y.push_back(elm3(iter->col,iter->row,0);
iter++;
while (iter!=x.end())
{
y.push_back(elm3(iter->col,iter->row,iter->d));
iter++;
}
SortMat(x);

}

void PrintMatrix(const parseMat & x,ostream os&=cout)
{
int i,j;
int m,n,d;
parseMat::interator iter =x.begin() ;
m =iter->row,n=iter->col;
iter++;
for(i=0;i {
if(iter ==x.end() || iter->row > i )
for (j=0;j {
os < }
else
{
for (j=0;j {
if( iter!=x.end() && iter->row ==i && iter->col==j )
{
os <d;
iter++;
}
else {os < }
} os }
}