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
| #include<iostream> #include<cstdlib> using namespace std;
typedef int ElemType; typedef bool Status;
typedef struct OLNode { int i, j; ElemType value; struct OLNode* right, * down; }OLNode, * OLink;
typedef struct { OLink* rhead, * chead; int cols, rows, nums; }CrossList;
Status CreateCrossList(CrossList& M); Status CrossListPrint(CrossList M); Status MatrixAdd(CrossList& A, CrossList B); Status DestroyCrossList(CrossList B); int main() { CrossList A, B; if (!CreateCrossList(A) || !CreateCrossList(B)) { cout << "Space allocate error!\n"; return 0; } cout << "After addiction:\n"; MatrixAdd(A, B); DestroyCrossList(B); CrossListPrint(A); return 0; }
Status CreateCrossList(CrossList& M) { cout << "Input Matrix rows, cols and nums:\n"; cin >> M.rows >> M.cols >> M.nums; cout << "Input Matrix elements:(x, y, value)\n";
if (!(M.rhead = (OLink*)malloc(M.rows * sizeof(OLink)))) return false; if (!(M.chead = (OLink*)malloc(M.cols * sizeof(OLink)))) return false;
for (int i = 0; i < M.rows; i++) M.rhead[i] = NULL; for (int i = 0; i < M.cols; i++) M.chead[i] = NULL;
int temp_i, temp_j, temp_value; for (cin >> temp_i >> temp_j >> temp_value; temp_i != 0; cin >> temp_i >> temp_j >> temp_value) { OLink p; if (!(p = (OLink)malloc(sizeof(OLNode)))) return false; p->i = temp_i; p->j = temp_j; p->value = temp_value;
if (M.rhead[temp_i - 1] == NULL || M.rhead[temp_i - 1]->j > temp_j) { p->right = M.rhead[temp_i - 1]; M.rhead[temp_i - 1] = p; } else { OLink q = NULL; for (q = M.rhead[temp_i - 1]; (q->right) && q->right->j < temp_j; q = q->right); p->right = q->right; q->right = p; }
if (M.chead[temp_j - 1] == NULL || M.chead[temp_j - 1]->i > temp_i) { p->down = M.chead[temp_j - 1]; M.chead[temp_j - 1] = p; } else { OLink q = NULL; for (q = M.chead[temp_j - 1]; (q->down) && q->down->i < temp_i; q = q->down); p->down = q->down; q->down = p; } } return true; }
Status MatrixAdd(CrossList &A, CrossList B) { if (A.rows != B.rows || A.cols != B.cols) return false;
OLink colptr[100]; for (int i = 0; i < A.cols; i++) colptr[i] = A.chead[i]; for (int k = 0; k < A.rows; k++) { OLink pa = A.rhead[k], pb = B.rhead[k], pre = NULL;
while (pb) { if (pa == NULL || pa->j > pb->j) { OLink p = (OLink)malloc(sizeof(OLNode)); if (!p) return false; p->i = pb->i; p->j = pb->j; p->value = pb->value;
if (pre == NULL) A.rhead[pb->i - 1] = p; else pre->right = p; p->right = pa; pre = p;
if (!A.chead[p->j - 1] || A.chead[p->j - 1]->i > p->i) { p->down = A.chead[p->j - 1]; A.chead[p->j - 1] = p; } else { p->down = colptr[p->j - 1]->down; colptr[p->j - 1]->down = p; } colptr[p->j - 1] = p; pb = pb->right; } else if (pa != NULL && pa->j < pb->j) { pre = pa; pa = pa->right; } else if (pa->j == pb->j) { pa->value += pb->value; if (pa->value == 0) { if (pre == NULL) A.rhead[pa->i - 1] = pa->right; else pre->right = pa->right; OLink p = pa; pa = pa->right; if (A.chead[p->j - 1] == p) A.chead[p->j - 1] = colptr[p->j - 1] = p->down; else colptr[p->j - 1]->down = p->down; free(p); } pb = pb->right; } } for (int i = 0; i < A.cols; i++) if (colptr[i] && colptr[i]->down && colptr[i]->down->j < k + 2) colptr[i] = colptr[i]->down; } return true; }
Status CrossListPrint(CrossList M) { cout << "Output Matrix:\n"; for (int i = 0; i < M.rows; i++) { for (int j = 0; j < M.cols; j++) { if (M.rhead[i] && M.rhead[i]->j == (j + 1)) { cout << M.rhead[i]->value << " "; M.rhead[i] = M.rhead[i]->right; } else cout << "0 "; } cout << "\n"; } return true; } Status DestroyCrossList(CrossList B) { for (int i = 0; i < B.rows; i++) { OLink p = B.rhead[i]; while (p) { OLink q = p; p = p->right; free(q); } } free(B.rhead); free(B.chead); return true; }
|