// DataStructTest.cpp : Defines the entry point for the console application.
//
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "stdafx.h"
#include <iostream.h>
#include <malloc.h>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
const int maxnum=10;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
struct NODE
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int rowNO; //行号
int colNO; //列号
int v;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
struct SpMatrixTp
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int mu; //行数
int nu; //列数
int tu; //非0元素个数
NODE data[maxnum+1]; //书上说假定三元组下标从1开始
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void ShowArray2(int * p,int row,int col)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int index=0;
for(int i=0;i<row;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int j=0;j<col;j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (j==0)
cout<<p[index++];
else
cout<<" "<<p[index++];
}
cout<<endl;
}
cout<<endl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//通过二维数组建立三元组
void CreateMatrix(int * p,int row,int col,SpMatrixTp & matrix)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
matrix.mu=0;
matrix.nu=0;
matrix.tu=0;
int index=1;
matrix.mu=row;
matrix.nu=col;
matrix.tu=0;
for(int i=0;i<row;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int j=0;j<col;j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (p[i*col+j]!=0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
matrix.tu++;
matrix.data[index].rowNO=i+1;
matrix.data[index].colNO=j+1;
matrix.data[index].v=p[i*col+j];
index++;
}
}
cout<<endl;
}
cout<<endl;
}
//显示三元组
void DisplayMatrix(SpMatrixTp & matrix)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
for(int i=0;i<matrix.tu;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout<<matrix.data[i+1].rowNO<<","<<matrix.data[i+1].colNO<<","<<matrix.data[i+1].v<<endl;
}
}
//按列序转置(非快速方法)
void trans_Sparmat(SpMatrixTp a,SpMatrixTp & b)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
b.mu=a.nu;
b.nu=a.mu;
b.tu=a.tu;
if (a.tu>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int index=1;
for(int col=1;col<=a.nu;col++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=1;i<=a.tu;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (a.data[i].colNO==col)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
b.data[index].colNO=a.data[i].rowNO;
b.data[index].rowNO=a.data[i].colNO;
b.data[index].v=a.data[i].v;
index++;
}
}
}
}
}
//快速转置算法
void Fast_Trans_Sparmat(SpMatrixTp a,SpMatrixTp & b)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
b.mu=a.nu;
b.nu=a.mu;
b.tu=a.tu;
//记录每列的非零元素个数申动动态空数用作数组,因为数组的维数不可以使用变量,a.nu+1是多申请了一个元素的空间,
//目的是使它与矩阵结构中下标向量以1起始相同,减少逻辑上的混乱
int * num=(int *)malloc(sizeof(int)*(a.nu+1));
//记录每列中第一个非零元素在转置后的三元组中的位置
int * cpot=(int *)malloc(sizeof(int)*(a.nu+1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (a.tu>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int col=0;
//初始化该数组,使所有元素都置0
for(col=1;col<=a.nu;col++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num[col]=0;
}
//记录矩阵中每一列的非零元素数
for(col=1;col<=a.nu;col++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num[a.data[col].colNO]++;
}
//记录原矩阵中第一个非0元素在转置后的三元组中位置
cpot[1]=1;
for(col=2;col<=a.nu;col++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cpot[col]=cpot[col-1]+num[col-1];
}
//进行转置操作
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//因为向量num记录了每列的非零元素的个数,cpot记录了每列中第1个非零元素在转置后的三元组中的位置,那么就可以
//使cpot[col]在每次循环中递增1,使得记录了当前col的下一个元素在转置三元组中的位置
for(int p=1;p<=a.tu;p++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int q=cpot[a.data[p].colNO];
b.data[q].colNO=a.data[p].rowNO;
b.data[q].rowNO=a.data[p].colNO;
b.data[q].v=a.data[p].v;
//记录第col列的下一个非零元素在转置后的三元组中的相应位置
cpot[a.data[p].colNO]++;
}
}
free(num);
free(cpot);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main(int argc, char* argv[])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
const int row=5,col=6;
int Array2[row][col]=
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{0,3,0,0,0,1},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{0,0,0,0,0,0},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{5,-1,0,0,0,0},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{0,0,0,0,4,0},
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{-3,0,0,0,0,0}
};
//建立三元组
SpMatrixTp matrix;
CreateMatrix(&Array2[0][0],row,col,matrix);
DisplayMatrix(matrix);
//使用转置算法1
SpMatrixTp transMatrix1;
trans_Sparmat(matrix,transMatrix1);
cout<<"以下是使用第一种转置算法生成的转置后的三元组,其时间复杂度为O(行数*列数)"<<endl;
DisplayMatrix(transMatrix1);
//使用转置算法2(快速转置算法)
cout<<"以下是使用第二种快速转置算法生成的转置后的三元组,其时间复杂度为O(列数+非零元素数)"<<endl;
SpMatrixTp transMatrix2;
Fast_Trans_Sparmat(matrix,transMatrix2);
DisplayMatrix(transMatrix2);
return 0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。