# 稀疏矩阵的基本操作

###稀疏矩阵的基本操作

IDE: Xcode

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAXSIZE 12500
typedef int ElemType;

typedef struct {
int i;//行下标
int j;//列下标
ElemType elem;//非零元
}Triple;

typedef struct {
Triple* triple;
int mu,nu,tu;//矩阵行数，列数，非零元数
}Matrix;

typedef Triple* TriplePtr;
typedef Matrix* MatrixPtr;

//初始化矩阵
MatrixPtr initMatrix(int row, int col, int valueCount) {
MatrixPtr matrix = (MatrixPtr)malloc(sizeof(Matrix));
matrix -> triple = (TriplePtr)calloc(sizeof(Triple), MAXSIZE);
matrix -> mu = row;
matrix -> nu = col;
matrix -> tu = valueCount;
return matrix;
}

//输入三元组对矩阵增加非零元
void insertElemToMatrix(MatrixPtr matrix) {
int row,col,value;
printf("请输入有效数据(三元组):\n");
for (int i = 1; i <= matrix -> tu; i++) {
scanf("%d,%d,%d",&row, &col, &value);
matrix -> triple[i].i = row;
matrix -> triple[i].j = col;
matrix -> triple[i].elem = value;
getchar();
}
}

//矩阵的转置
MatrixPtr transposeMatrix(MatrixPtr matrix) {
int q = 1;
MatrixPtr Tmatrix = initMatrix(matrix -> nu, matrix -> mu, matrix -> tu);
for (int row = 1; row <= matrix -> mu; row++) {
for (int p = 1; p <= matrix -> tu; p++) {
if (matrix -> triple[p].i == row) {
Tmatrix -> triple[q].i = matrix -> triple[p].j;
Tmatrix -> triple[q].j = matrix -> triple[p].i;
Tmatrix -> triple[q].elem = matrix -> triple[p].elem;
q++;
}
}
}
return Tmatrix;
}

//快速转置
MatrixPtr fastTransposeMatrix(MatrixPtr matrix) {
MatrixPtr Tmatrix = initMatrix(matrix -> nu, matrix -> mu, matrix -> tu);
int col,q;
int num[matrix -> nu],cpot[matrix -> nu];
for (col = 1; col <= matrix -> nu; col++) { num[col] = 0; } //初始化num数组
for (int t = 1; t <= matrix -> tu; t++) { ++num[matrix -> triple[t].j]; } //求矩阵M中的每一列的非零元数
cpot[1] = 1;
for (col = 2; col <= matrix -> nu; col++) { cpot[col] = cpot[col - 1] + num[col - 1]; }
for (int p = 1; p <= matrix -> tu; p++) {
col = matrix -> triple[p].j;
q = cpot[col];
Tmatrix -> triple[q].i = matrix -> triple[p].j;
Tmatrix -> triple[q].j = matrix -> triple[p].i;
Tmatrix -> triple[q].elem = matrix -> triple[p].elem;
cpot[col]++;
}
return Tmatrix;
}

//遍历矩阵
void traverseMatrix(MatrixPtr matrix, void (* visit)(ElemType data)) {
int index = 1;
for (int i = 1; i <= matrix -> mu; i++) {
for (int j = 1; j <= matrix -> nu; j ++) {
if (matrix -> triple[index].i == i && matrix -> triple[index].j == j) {
visit(matrix -> triple[index++].elem);
} else {
visit(0);
}
}
printf("\n");
}
}

//打印方法
void printMatrix(ElemType data) {
printf("%4d",data);
}

int main(int argc, const char * argv[]) {
MatrixPtr matrix = initMatrix(6, 5, 3);
insertElemToMatrix(matrix);
printf("打印矩阵如下：\n");
traverseMatrix(matrix, printMatrix);

printf("-------------------------\n");
MatrixPtr TMatrix = transposeMatrix(matrix);
printf("转置后矩阵如下：\n");
traverseMatrix(TMatrix, printMatrix);

printf("-------------------------\n");
MatrixPtr fastTMatrix = fastTransposeMatrix(matrix);
printf("快速转置后矩阵如下：\n");
traverseMatrix(fastTMatrix, printMatrix);

return 0;
}

• 1
点赞
• 1
评论
• 8
收藏
• 一键三连
• 扫一扫，分享海报

09-01

06-21 1389
12-20 1776
11-12 3467
10-27 1万+
03-18 4801
06-15
12-20
12-13
12-26
12-29
08-13 1144
06-03 927
07-02
11-30 1620
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客

¥2 ¥4 ¥6 ¥10 ¥20

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