数据结构-C语言代码 day10-压缩矩阵的转置

压缩矩阵:指为多个值相同的元素只分配一个存储空间,对零元素不分配存储空间。

转置:将<i,j>元素与<j,i>元素互换。

 

1.定义结构体

typedef struct Triple
{
    int i;//行
    int j;//列
    elem e;//元素值
} Triple, *TriplePtr;

typedef struct CompressedMatrix
{
    int rows,columns,numElements;
    Triple* elements;
} CompressedMatrix, *CompressedMatrixPtr;

 2.压缩矩阵

CompressedMatrixPtr initCompressedMatrix(int paraRows, int paraColumns, int paraElements, int** paraData)
{
	int i;
	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
	resultPtr->rows = paraRows;
	resultPtr->columns = paraColumns;
	resultPtr->numElements = paraElements;
	resultPtr->elements = (TriplePtr)malloc(paraElements * sizeof(struct Triple));

	for(i = 0; i < paraElements; i ++)
    {
		resultPtr->elements[i].i = paraData[i][0];
		resultPtr->elements[i].j = paraData[i][1];
		resultPtr->elements[i].e = paraData[i][2];
	}

	return resultPtr;
}

3.打印矩阵 

void printCompressedMatrix(CompressedMatrixPtr paraPtr)
{
	int i;
	for(i = 0; i < paraPtr->numElements; i ++)
    {
		printf("(%d, %d): %d\r\n", paraPtr->elements[i].i, paraPtr->elements[i].j, paraPtr->elements[i].e);
	}
}

4.转置压缩矩阵 

CompressedMatrixPtr transposeCompressedMatrix(CompressedMatrixPtr paraPtr)
{
	//第一步:申请空间
	int i, tempColumn, tempPosition;
	int *tempColumnCounts = (int*)malloc(paraPtr->columns * sizeof(int));
	int *tempOffsets = (int*)malloc(paraPtr->columns * sizeof(int));
	for(i = 0; i < paraPtr->columns; i ++)
    {
		tempColumnCounts[i] = 0;
	}

	CompressedMatrixPtr resultPtr = (CompressedMatrixPtr)malloc(sizeof(struct CompressedMatrix));
    //赋值
	resultPtr->rows = paraPtr->columns;
	resultPtr->columns = paraPtr->rows;
	resultPtr->numElements = paraPtr->numElements;

	resultPtr->elements = (TriplePtr)malloc(paraPtr->numElements * sizeof(struct Triple));
	
	//第二步:一次扫描以计算偏移量
	for(i = 0; i < paraPtr->numElements; i ++) 
    {
		tempColumnCounts[paraPtr->elements[i].j] ++;
	} 
	tempOffsets[0] = 0;
	for(i = 1; i < paraPtr->columns; i ++)
    {
		tempOffsets[i] = tempOffsets[i - 1] + tempColumnCounts[i - 1];
		printf("tempOffsets[%d] = %d \r\n", i, tempOffsets[i]);
	}

	//第三步:再次扫描以填充数据
	for(i = 0; i < paraPtr->numElements; i ++)  
    {
		tempColumn = paraPtr->elements[i].j;
		tempPosition = tempOffsets[tempColumn];
		resultPtr->elements[tempPosition].i = paraPtr->elements[i].j;
		resultPtr->elements[tempPosition].j = paraPtr->elements[i].i;
		resultPtr->elements[tempPosition].e = paraPtr->elements[i].e;

		tempOffsets[tempColumn]++;
	}

	return resultPtr;
}

 5.测试函数

void compressedMatrixTest()
{
	CompressedMatrixPtr tempPtr1, tempPtr2;
	int i, j, tempElements;

	//构造第一个样本矩阵
	tempElements = 4;
	int** tempMatrix1 = (int**)malloc(tempElements * sizeof(int*));
	for(i = 0; i < tempElements; i ++)
    {
		tempMatrix1[i] = (int*)malloc(3 * sizeof(int));
	}

	int tempMatrix2[4][3] = {{0, 0, 2}, {0, 2, 3}, {2, 0, 5}, {2, 1, 6}};
	for(i = 0; i < tempElements; i ++)
    {
		for(j = 0; j < 3; j ++) 
        {
			tempMatrix1[i][j] = tempMatrix2[i][j];
		}
	}
	
	tempPtr1 = initCompressedMatrix(2, 3, 4, tempMatrix1);

	printf("初始化后.\r\n");
	printCompressedMatrix(tempPtr1);

	tempPtr2 = transposeCompressedMatrix(tempPtr1);
	printf("转置后.\r\n");
	printCompressedMatrix(tempPtr2);
}

初始化后.
(0, 0): 2
(0, 2): 3
(2, 0): 5
(2, 1): 6
tempOffsets[1] = 2
tempOffsets[2] = 3
转置后.
(0, 0): 2
(0, 2): 5
(1, 2): 6
(2, 0): 3

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值