稀疏矩阵的三元组顺序表示方法及基本操作的实现(建立、输出、转置)并实现一个主菜单来实现。实现稀疏矩阵的三元组表示下的普通转置、快速转置。给我C语言完整代码
时间: 2024-05-04 10:17:17 浏览: 40
以下是稀疏矩阵的三元组顺序表示方法及基本操作的实现(建立、输出、转置)的C语言完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int row, col, val;
} Triple;
typedef struct {
Triple data[MAXSIZE + 1];
int rows, cols, nums;
} Matrix;
void createMatrix(Matrix *matrix) {
printf("请输入矩阵的行数和列数:");
scanf("%d%d", &matrix->rows, &matrix->cols);
printf("请输入矩阵的非零元素个数:");
scanf("%d", &matrix->nums);
printf("请输入所有非零元素的行号、列号、值:\n");
for (int i = 1; i <= matrix->nums; i++) {
scanf("%d%d%d", &matrix->data[i].row, &matrix->data[i].col, &matrix->data[i].val);
}
}
void printMatrix(Matrix matrix) {
printf("矩阵的三元组顺序表示如下:\n");
printf("行数:%d,列数:%d,非零元素个数:%d\n", matrix.rows, matrix.cols, matrix.nums);
for (int i = 1; i <= matrix.nums; i++) {
printf("%d %d %d\n", matrix.data[i].row, matrix.data[i].col, matrix.data[i].val);
}
}
void transposeMatrix(Matrix matrix, Matrix *transposeMatrix) {
transposeMatrix->cols = matrix.rows;
transposeMatrix->rows = matrix.cols;
transposeMatrix->nums = matrix.nums;
if (transposeMatrix->nums == 0) {
return;
}
int *numCount = (int *) calloc(matrix.cols + 1, sizeof(int));
for (int i = 1; i <= matrix.nums; i++) {
numCount[matrix.data[i].col]++;
}
int *pos = (int *) calloc(matrix.cols + 1, sizeof(int));
for (int i = 1; i <= matrix.cols; i++) {
pos[i] = pos[i - 1] + numCount[i - 1];
}
for (int i = 1; i <= matrix.nums; i++) {
int j = pos[matrix.data[i].col]++;
transposeMatrix->data[j].row = matrix.data[i].col;
transposeMatrix->data[j].col = matrix.data[i].row;
transposeMatrix->data[j].val = matrix.data[i].val;
}
free(numCount);
free(pos);
}
int main() {
Matrix matrix, transposeMatrix;
createMatrix(&matrix);
printMatrix(matrix);
transposeMatrix(matrix, &transposeMatrix);
printf("原矩阵的普通转置矩阵如下:\n");
printMatrix(transposeMatrix);
transposeMatrix(transposeMatrix, &matrix);
printf("原矩阵的快速转置矩阵如下:\n");
printMatrix(matrix);
return 0;
}
```
程序中定义了一个`Triple`结构体和一个`Matrix`结构体,`Triple`结构体定义了三元组的行、列和值,`Matrix`结构体则定义了三元组表示法的矩阵的行数、列数、非零元素个数以及所有的三元组。
程序中实现了三个函数:`createMatrix()`用于创建三元组表示法的矩阵;`printMatrix()`用于输出三元组顺序表示法的矩阵;`transposeMatrix()`用于实现普通转置和快速转置。
在`transposeMatrix()`函数中,首先根据原矩阵的列数动态分配两个数组:`numCount`数组用于统计每列非零元素的个数,`pos`数组用于存储每列第一个非零元素在转置矩阵中的位置。然后遍历原矩阵中的所有三元组,根据`pos`数组将每个三元组插入到转置矩阵中的正确位置。最后释放动态分配的数组。
最后在`main()`函数中,先调用`createMatrix()`函数创建一个三元组表示法的矩阵,然后调用`printMatrix()`函数输出该矩阵,接着调用`transposeMatrix()`函数实现普通转置,再调用`printMatrix()`函数输出转置矩阵,最后再次调用`transposeMatrix()`函数实现快速转置,再调用`printMatrix()`函数输出转置后的矩阵。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)