【数据结构】 三元组的转置

前端之家收集整理的这篇文章主要介绍了【数据结构】 三元组的转置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#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,};
	TsMatrix chaMatrix = {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);
	}

	// 转置并输出
	chaMatrix.num = matrix.num;
	chaMatrix.row = matrix.val;
	chaMatrix.val = matrix.row;

	m = 0;

	for ( int i = 0; i < matrix.val; i++)
	{
		for (int j = 0; j <= matrix.num; j++)
		{
			if (i == matrix.data[j].y)
			{
				chaMatrix.data[m].x =  matrix.data[j].y;
				chaMatrix.data[m].y =  matrix.data[j].x;
				chaMatrix.data[m].value =  matrix.data[j].value;

				m++;
			}
		}
	}

	printf("\n**********转置后************\n");

	for (int i = 0; i < m; i++)
	{
		printf("%d %d %d\n",chaMatrix.data[i].x,chaMatrix.data[i].y,chaMatrix.data[i].value);
	}
}
原文链接:https://www.f2er.com/datastructure/383093.html

猜你在找的数据结构相关文章