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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
| #include <stdio.h> #include <stdlib.h>
typedef struct OLNode { int row; int col; int value;
struct OLNode *right; struct OLNode *down; } OLNode;
typedef struct CrossList { OLNode** row_heads; OLNode** col_heads; int rows; int cols; int num_elements; } CrossList;
CrossList* initCrossList(int m_rows, int m_cols) { CrossList* matrix = (CrossList*)malloc(sizeof(CrossList)); if (matrix == NULL) { printf("内存分配失败!\n"); exit(1); } matrix->rows = m_rows; matrix->cols = m_cols; matrix->num_elements = 0;
matrix->row_heads = (OLNode**)malloc(sizeof(OLNode*) * m_rows); if (matrix->row_heads == NULL) { } for (int i = 0; i < m_rows; i++) { matrix->row_heads[i] = NULL; }
matrix->col_heads = (OLNode**)malloc(sizeof(OLNode*) * m_cols); if (matrix->col_heads == NULL) { } for (int i = 0; i < m_cols; i++) { matrix->col_heads[i] = NULL; } return matrix; }
void insertElement(CrossList* matrix, int row, int col, int value) { if (row < 0 || row >= matrix->rows || col < 0 || col >= matrix->cols) { printf("插入失败:索引越界 (%d, %d)。\n", row, col); return; } if (value == 0) { return; }
OLNode* newNode = (OLNode*)malloc(sizeof(OLNode)); if (newNode == NULL) { printf("内存分配失败!\n"); exit(1); } newNode->row = row; newNode->col = col; newNode->value = value; newNode->right = NULL; newNode->down = NULL;
OLNode* p_row = matrix->row_heads[row]; OLNode* prev_row = NULL; while (p_row != NULL && p_row->col < col) { prev_row = p_row; p_row = p_row->right; } if (p_row != NULL && p_row->col == col) { p_row->value = value; free(newNode); return; }
if (prev_row == NULL) { newNode->right = matrix->row_heads[row]; matrix->row_heads[row] = newNode; } else { newNode->right = prev_row->right; prev_row->right = newNode; }
OLNode* p_col = matrix->col_heads[col]; OLNode* prev_col = NULL; while (p_col != NULL && p_col->row < row) { prev_col = p_col; p_col = p_col->down; }
if (prev_col == NULL) { newNode->down = matrix->col_heads[col]; matrix->col_heads[col] = newNode; } else { newNode->down = prev_col->down; prev_col->down = newNode; }
matrix->num_elements++; }
int getElement(CrossList* matrix, int row, int col) { if (row < 0 || row >= matrix->rows || col < 0 || col >= matrix->cols) { printf("查找失败:索引越界。\n"); return 0; } OLNode* current = matrix->row_heads[row]; while (current != NULL && current->col < col) { current = current->right; } if (current != NULL && current->col == col) { return current->value; } return 0; }
void deleteElement(CrossList* matrix, int row, int col) { if (row < 0 || row >= matrix->rows || col < 0 || col >= matrix->cols) { printf("删除失败:索引越界。\n"); return; }
OLNode* nodeToDelete = NULL;
OLNode* prev_row = NULL; OLNode* current_row = matrix->row_heads[row]; while (current_row != NULL && current_row->col < col) { prev_row = current_row; current_row = current_row->right; } if (current_row != NULL && current_row->col == col) { nodeToDelete = current_row; if (prev_row == NULL) { matrix->row_heads[row] = current_row->right; } else { prev_row->right = current_row->right; } } else { printf("未找到 (%d, %d) 处的非零元素进行删除。\n", row, col); return; }
OLNode* prev_col = NULL; OLNode* current_col = matrix->col_heads[col]; while (current_col != NULL && current_col->row < row) { prev_col = current_col; current_col = current_col->down; } if (current_col != NULL && current_col->row == row && current_col->col == col) { if (prev_col == NULL) { matrix->col_heads[col] = current_col->down; } else { prev_col->down = current_col->down; } } free(nodeToDelete); matrix->num_elements--; printf("成功删除元素 (%d, %d)。\n", row, col); }
void printFullMatrix(CrossList* matrix) { printf("\n完整矩阵 (%d x %d):\n", matrix->rows, matrix->cols); for (int i = 0; i < matrix->rows; i++) { OLNode* current_row_node = matrix->row_heads[i]; for (int j = 0; j < matrix->cols; j++) { if (current_row_node != NULL && current_row_node->col == j) { printf("%3d ", current_row_node->value); current_row_node = current_row_node->right; } else { printf("%3d ", 0); } } printf("\n"); } }
void freeCrossList(CrossList* matrix) { if (matrix == NULL) return;
for (int i = 0; i < matrix->rows; i++) { OLNode* current = matrix->row_heads[i]; while (current != NULL) { OLNode* temp = current; current = current->right; free(temp); } }
free(matrix->row_heads); free(matrix->col_heads); free(matrix); printf("十字链表内存已释放。\n"); }
int main() { int rows = 4, cols = 5; CrossList* sparseMatrix = initCrossList(rows, cols);
printf("------ 插入元素 ------\n"); insertElement(sparseMatrix, 0, 1, 5); insertElement(sparseMatrix, 0, 4, 8); insertElement(sparseMatrix, 1, 0, 12); insertElement(sparseMatrix, 1, 3, 7); insertElement(sparseMatrix, 2, 1, 9); insertElement(sparseMatrix, 3, 2, 6); insertElement(sparseMatrix, 3, 4, 10); printf("插入重复位置元素 (更新):\n"); insertElement(sparseMatrix, 0, 1, 100);
printFullMatrix(sparseMatrix);
printf("\n------ 查找元素 ------\n"); printf("元素 (0, 1): %d\n", getElement(sparseMatrix, 0, 1)); printf("元素 (1, 2): %d\n", getElement(sparseMatrix, 1, 2)); printf("元素 (3, 4): %d\n", getElement(sparseMatrix, 3, 4));
printf("\n------ 删除元素 ------\n"); deleteElement(sparseMatrix, 0, 4); printFullMatrix(sparseMatrix);
deleteElement(sparseMatrix, 1, 0); printFullMatrix(sparseMatrix);
deleteElement(sparseMatrix, 2, 2); printFullMatrix(sparseMatrix);
printf("\n------ 内存释放 ------\n"); freeCrossList(sparseMatrix);
return 0; }
|