C++ 为什么不执行此链表代码?

C++ 为什么不执行此链表代码?,c++,linked-list,C++,Linked List,我不知道为什么这个代码不起作用 #include <iostream> using namespace std; struct Triple {int row, col, value;}; class Matrix; class MatrixNode { friend class Matrix; friend istream& operator>>(istream&, Matrix&); private: MatrixNode *

我不知道为什么这个代码不起作用

#include <iostream>

using namespace std;

struct Triple {int row, col, value;};

class Matrix;

class MatrixNode 
{
friend class Matrix;
friend istream& operator>>(istream&, Matrix&);
private:
    MatrixNode *down, *right;
    bool head;
    union {
        MatrixNode *next;
        Triple triple;
    };
    MatrixNode(bool, Triple*);
};

MatrixNode::MatrixNode(bool b, Triple* t)
{
    head = b;
    if (b) {right = down = this;}
    else triple = *t;
}

class Matrix {
friend istream& operator>>(istream&, Matrix&);
public:
    ~Matrix();
private:
    MatrixNode *headnode;
};

istream& operator>>(istream& is, Matrix& matrix)
{
    Triple s;
    cout << "Enter the numbers of row, colume, value" << endl;
    is >> s.row >> s.col >> s.value;

    int p = max(s.row, s.col);

    matrix.headnode = new MatrixNode(false, &s);
    if (p == 0) { matrix.headnode->right = matrix.headnode; return is;}
    MatrixNode **head = new MatrixNode*[p];
    for (int i = 0; i < p; i++)
        head[i] = new MatrixNode(true, 0);
    int currentRow=0;
    MatrixNode *last = head[0];

    for (int i = 0; i < s.value; i++)
    {
        Triple t;
        cout << "Enter the terms" << endl;
        is >> t.row >> t.col >> t.value;
        if (t.row > currentRow){
            last -> right = head[currentRow];
            currentRow = t.row;
            last = head[currentRow];
        }
        last = last -> right = new MatrixNode(false, &t);
        head[t.col]->next = head[t.col]->next->down = last;
    }

    last -> right = head[currentRow];
    for (int i = 0; i < s.col; i++)
        head[i] -> next -> down = head[i];
    for(int i = 0; i < p - 1; i++)
        head[i] -> next = head[i+1];
    head[p-1] -> next = matrix.headnode;
    matrix.headnode -> right = head[0];
    delete [] head;
    return is;
}


int main()
{
    Matrix *a = new Matrix;
    Matrix *b = new Matrix;

    cin >> *a;
    cin >> *b;

}
#包括
使用名称空间std;
结构三元组{int row,col,value;};
类矩阵;
类矩阵节点
{
友元类矩阵;
friend istream&operator>>(istream&,Matrix&);
私人:
矩阵节点*向下,*向右;
布尔头;
联合{
MatrixNode*下一步;
三重;
};
矩阵节点(bool,Triple*);
};
MatrixNode::MatrixNode(布尔b,三重*t)
{
水头=b;
如果(b){right=down=this;}
else三元组=*t;
}
类矩阵{
friend istream&operator>>(istream&,Matrix&);
公众:
~Matrix();
私人:
矩阵节点*头节点;
};
istream和运算符>>(istream和is、矩阵和矩阵)
{
三s;
cout s.row>>s.col>>s.value;
int p=最大值(标准行、标准列);
matrix.headnode=新的MatrixNode(false,&s);
如果(p==0){matrix.headnode->right=matrix.headnode;返回为;}
MatrixNode**head=新MatrixNode*[p];
对于(int i=0;i>t.col>>t.value;
如果(t.row>currentRow){
最后->右=头部[当前行];
currentRow=t.row;
last=头[当前行];
}
last=last->right=newmatrixnode(false,&t);
头部[t.col]->next=头部[t.col]->next->down=最后一个;
}
最后->右=头部[当前行];
for(int i=0;i下一步->向下=头部[i];
对于(int i=0;i下一个=头部[i+1];
head[p-1]->next=matrix.headnode;
matrix.headnode->right=头[0];
删除[]标题;
回报是;
}
int main()
{
矩阵*a=新矩阵;
矩阵*b=新矩阵;
cin>>*a;
cin>>*b;
}

具体问题是
head[t.col]->next
未初始化,您正在取消引用它


更普遍的问题是,你吃得太多了。你正试图做一些超出你能力范围的事情,你正试图一步到位。尝试构建一个简单得多的链表,比如说,一个没有并集、没有操作符>>、只有一个维度而不是三个维度的链表。一旦你对此感到满意,你就可以一小步一小步地升级到更高级的结构。

什么让你认为它不起作用?你看到了什么错误?你能举一个例子说明这个错误吗?你能简化这段代码并且仍然得到错误吗?这部分会出错。但我不知道为什么。请帮我引导[t.col]->next=引导[t.col]->next->down=最后;这似乎是一些非常聪明的代码。它想做什么?非常感谢。我会记住的。