稀疏矩阵的加法运算

输入任意两个稀疏矩阵A和B,可以求出它们的和矩阵C。

  • 输入两个稀疏矩阵的行数、列数以及非零元素,创建稀疏矩阵
  • 实现两个稀疏矩阵的加法运算
  • 输出稀疏矩阵
  • 要求用两种数据结构实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row;
int col;
int value;
} Element;

typedef struct {
int rows;
int cols;
int count;
Element* elements;
} SparseMatrix;

SparseMatrix* createSparseMatrix(int rows, int cols, int count) {
SparseMatrix* matrix = (SparseMatrix*)malloc(sizeof(SparseMatrix));
matrix->rows = rows;
matrix->cols = cols;
matrix->count = count;
matrix->elements = (Element*)malloc(count * sizeof(Element));
return matrix;
}

void addElement(SparseMatrix* matrix, int row, int col, int value) {
if (matrix->count >= matrix->rows * matrix->cols) {
printf("Matrix is full. Cannot add more elements.\n");
return;
}

Element element = {row, col, value};
matrix->elements[matrix->count++] = element;
}

SparseMatrix* addSparseMatrices(const SparseMatrix* matrixA, const SparseMatrix* matrixB) {
if (matrixA->rows != matrixB->rows || matrixA->cols != matrixB->cols) {
printf("Matrix dimensions do not match.\n");
return NULL;
}

SparseMatrix* resultMatrix = createSparseMatrix(matrixA->rows, matrixA->cols, 0);

int i = 0, j = 0;
while (i < matrixA->count && j < matrixB->count) {
Element elementA = matrixA->elements[i];
Element elementB = matrixB->elements[j];

if (elementA.row < elementB.row || (elementA.row == elementB.row && elementA.col < elementB.col)) {
addElement(resultMatrix, elementA.row, elementA.col, elementA.value);
i++;
} else if (elementA.row > elementB.row || (elementA.row == elementB.row && elementA.col > elementB.col)) {
addElement(resultMatrix, elementB.row, elementB.col, elementB.value);
j++;
} else {
int sum = elementA.value + elementB.value;
if (sum != 0) {
addElement(resultMatrix, elementA.row, elementA.col, sum);
}
i++;
j++;
}
}

while (i < matrixA->count) {
Element elementA = matrixA->elements[i];
addElement(resultMatrix, elementA.row, elementA.col, elementA.value);
i++;
}
while (j < matrixB->count) {
Element elementB = matrixB->elements[j];
addElement(resultMatrix, elementB.row, elementB.col, elementB.value);
j++;
}
return resultMatrix;
}
void printSparseMatrix(const SparseMatrix* matrix) {
for (int i = 0; i < matrix->count; i++) {
Element element = matrix->elements[i];
printf("(%d, %d): %d\n", element.row, element.col, element.value);
}
}
int main() {
// 创建稀疏矩阵A
SparseMatrix* matrixA = createSparseMatrix(3, 3, 3);
addElement(matrixA, 0, 0, 1);
addElement(matrixA, 1, 1, 2);
addElement(matrixA, 2, 2, 3);
// 创建稀疏矩阵B
SparseMatrix* matrixB = createSparseMatrix(3, 3, 2);
addElement(matrixB, 0, 0, 4);
addElement(matrixB, 2, 2, 5);
// 计算和矩阵C
SparseMatrix* matrixC = addSparseMatrices(matrixA, matrixB);
// 打印结果矩阵C
printSparseMatrix(matrixC);
// 释放内存
free(matrixA->elements);
free(matrixA);
free(matrixB->elements);
free(matrixB);
free(matrixC->elements);
free(matrixC);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
输入矩阵 A 的行数和列数: 3 3
输入矩阵 A 的非零元素(行列值):
0 0 1
1 1 2
2 2 3
输入矩阵 B 的行数和列数: 3 3
输入矩阵 B 的非零元素(行列值):
0 0 4
1 1 5
2 2 6
结果矩阵 C:
Row Column Value
0 0 5
1 1 7
2 2 9