Я пытаюсь написать код для ортогонально связанной разреженной матрицы.
Вот вопрос:
Альтернативное связанное представление для разреженных матриц использует узлы, которые имеют поля down, right, row, col и value. Каждая ненулевая запись разреженной матрицы представлена
по узлу. Нулевые члены не хранятся в явном виде. Узлы связаны между собой, образуя два круговых списка. Первый список, список строк, составляется путем связывания узлов по строкам и внутри строк по столбцам с использованием правого поля. Второй, список, список столбцов, составляется путем связывания узлов через поле вниз. В этом списке узлы связаны столбцами, а внутри столбцов - строками. Эти два списка имеют общий заголовочный узел. Кроме того, узел добавляется в размеры матрицы.
Входной файл выглядит так:
// Матрица A
4 4 7
1 1 2
1 4 1
2 2 7
3 1 9
3 3 8
4 2 4
4 3 5
// Матрица B
4 4 5
1 3 4
2 1 6
2 3 3
3 2 5
4 4 9
Это мой код для оператора >>:
istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x){
in >> x.numRows >> x.numCols >> x.numTerms;
in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;
x.push_back(x.currentNode);
if((x.currentNode->row == 1)&&(x.currentNode->col == 1)){
x.hnode->right = x.currentNode;
x.hnode->down = x.currentNode;
}
if(x.currentNode->col == 1){
x.hnode->down = x.currentNode;
}
if(x.currentNode->row == 1){
x.hnode->right = x.currentNode;
}
for (int i = 2; i <= x.numTerms; i++) {
in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;
x.push_back(x.currentNode);
}
return in;
}
Отлично компилируется. Но когда я пытаюсь запустить его, я получаю ошибку ошибки сегментации.
Может кто-нибудь помочь ??
Огромное спасибо!
Вот OrthogonalLinkedSparseMatrix.h:
#ifndef O_L_SPARSE_MATRIX_H
#define O_L_SPARSE_MATRIX_H
#include <iostream>
#include <fstream>
#include "node.h"
#include "myExceptions.h"
using namespace std;
class OrthogonalLinkedSparseMatrix;
ostream& operator<< (ostream&, OrthogonalLinkedSparseMatrix&);
istream& operator>> (istream&, OrthogonalLinkedSparseMatrix&);
class OrthogonalLinkedSparseMatrix{
public:
friend ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x);
friend istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x);
OrthogonalLinkedSparseMatrix(){}
~OrthogonalLinkedSparseMatrix(){}
void transpose(OrthogonalLinkedSparseMatrix &b);
void add(OrthogonalLinkedSparseMatrix &a, OrthogonalLinkedSparseMatrix &c);
void push_back(matrixNode *&mat_Node);
void setDowns(matrixNode *&mat_Node);
private:
matrixNode *hnode;
int numRows, numCols, numTerms;
matrixNode *currentNode;
matrixNode *previousNode;
matrixNode *nextNode;
};
// code for operator >> & <<, etc goes here, but everything's commented out except operator >>
#endif
РЕДАКТИРОВАТЬ: я включаю оператор <<, а также: </p>
ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x){
if(x.numTerms == 0){
out << "No non-zero terms" << endl;
return out;
}
out << x.numRows << x.numCols << x.numTerms << endl;
for (int i = 0; i < x.numTerms; i++) {
out << x.currentNode->row << x.currentNode->col << x.currentNode->value << endl;
}
return out;
}