#include <stdio.h>
#include <stdlib.h>
#dene MAXSIZE 1000
typedef int ElemType;
//定义三元组
typedef struct
{
int r, c;//行号,列号
ElemType e;//元素值
}Triple;
//定义三元组的顺序表
typedef struct
{
Triple data[MAXSIZE];
int rows, cols, num; //矩阵的行数,列数及非零元素个数
}TSMatrix;
// 稀疏矩阵的压缩存储
TSMatrix compressMatrix(ElemType **array, int r, int c)
{
int i,j,k=0;
Triple data[MAXSIZE];
TSMatrix *A = new TSMatrix;
A->num = 0;
A->rows = r;
A->cols = c;
for (i=0; i<A->rows; i++)
{
for (j=0; j<A->cols; j++)
{
if (array[i][j] != 0)
{
data[k] = *(new Triple);
data[k].e = array[i][j];
data[k].r = i;
data[k].c = j;
A->num++;
A->data[k] = data[k];
k++;
}
}
}
return *A;
}
评论0
最新资源