前端之家收集整理的这篇文章主要介绍了
【数据结构】 稀疏矩阵 转换成三元组,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
# include <stdio.h>
# define maxsize 100
//三元组结点:
typedef int datatype;
typedef struct
{
int x;
int y;
datatype value;
} Triple;
//稀疏矩阵:
typedef struct
{
Triple data[maxsize];
int row;
int val;
int num;/*行、列、非零元素个数*/
} TsMatrix;
void main()
{
int m = 1;
TsMatrix matrix = {0,};
int array[8][8] =
{
0,2,1,3,5,8,6,};
matrix.row = 8;
matrix.val = 8;
for (int i = 0; i < 8; i ++)
{
for (int j = 0; j < 8; j++)
{
if (array[i][j])
{
matrix.num ++;
matrix.data[m].x = i;
matrix.data[m].y = j;
matrix.data[m].value = array[i][j];
m++;
}
}
}
for (int i = 0; i < m; i++)
{
printf("%d %d %d\n",matrix.data[i].x,matrix.data[i].y,matrix.data[i].value);
}
}
原文链接:https://www.f2er.com/datastructure/383094.html