C++ 调试此程序失败(使用链表的稀疏矩阵)
我的函数运算符+不能正常工作。其他函数正常。我已经在这个程序上工作了一整天。它仍然不能正常工作 它只适用于一些特殊情况,如对角矩阵。希望有人能给我一些提示 矩阵的概念: 执行过程: 输入(行、列、值)。C++ 调试此程序失败(使用链表的稀疏矩阵),c++,matrix,data-structures,linked-list,C++,Matrix,Data Structures,Linked List,我的函数运算符+不能正常工作。其他函数正常。我已经在这个程序上工作了一整天。它仍然不能正常工作 它只适用于一些特殊情况,如对角矩阵。希望有人能给我一些提示 矩阵的概念: 执行过程: 输入(行、列、值)。 3//输入稀疏矩阵的维数和数目 0 0 1//稀疏矩阵1 1 2//稀疏矩阵3. 流的0,0,1//。 1,1,1 2,2,1 输入(行、列、值) 3 01 1 2 1 3 3. //输入添加维度 0,0,2//结果,此结果是正确的。 1,1,2 2,2,2 #包括 使用namespace::s
3//输入稀疏矩阵的维数和数目
0 0 1//稀疏矩阵1
1
2//稀疏矩阵3.
流的0,0,1//。
1,1,1
2,2,1
输入(行、列、值)
3
01
1
2 1
3 3. //输入添加维度
0,0,2//结果,此结果是正确的。
1,1,2
2,2,2
#包括
使用namespace::std;
结构三元组{
int行;
int col;
int-val;
};
类矩阵;
类矩阵节点{
友元类矩阵;
friend istream&operator>>(istream&is,Matrix&Matrix);
friend ostream&operator(istream&is,Matrix&Matrix);
friend ostream&operatorheadnode->右侧;
while(头节点!=此->头节点){
对于(Matrixnode*cur=headnode->right;cur!=headnode;cur=cur->right)
交换(cur->triple.row,cur->triple.col);
headnode=headnode->next;
}
归还*这个;
}
矩阵::运算符+(矩阵和矩阵){
Matrixnode*cur1=头部节点->右侧;
Matrixnode*cur2=矩阵头节点->右;
Matrixnode*headnode1=cur1->右侧;
Matrixnode*headnode2=cur2->右侧;
矩阵加法;
三s;
cin>>s.row>>s.col;
s、 val=10;
int p=最大值(标准行、标准列);
int x=矩阵.headnode->triple.val;
int y=头节点->triple.val;
add.headnode=新矩阵节点(false,&s);
如果(p==0)
{
add.headnode->right=add.headnode;
返回添加;
}
Matrixnode**head=新Matrixnode*[p];
对于(int i=0;itriple.row>currentRow&&headnode2->triple.row>currentRow)
{
最后->右=头部[当前行];
if(头节点1->triple.row>头节点2->triple.row){
currentRow=头节点1->triple.row;
last=头[当前行];
}
否则{
currentRow=头节点2->triple.row;
last=头[当前行];
}
}
if(headnode1->triple.col==headnode2->triple.col&&headnode1->triple.row==headnode2->triple.row){
三重t;
t、 val=headnode1->triple.val+headnode2->triple.val;
t、 col=头节点1->triple.col;
t、 行=头节点1->triple.row;
last=last->right=newmatrixnode(false,&t);
头部[t.col]->next=头部[t.col]->next->down=最后一个;
headnode1=headnode1->右侧;
headnode2=headnode2->右侧;
if(headnode1==cur1&&cur1!=headnode){
cur1=cur1->next;
headnode1=cur1->右侧;
}
if(headnode2==cur2&&cur2!=matrix.headnode){
cur2=cur2->next;
headnode2=cur2->right;
}
}
else if(headnode1->triple.col>headnode2->triple.col){
last=last->right=newmatrixnode(false,&headnode2->triple);
head[headnode2->triple.col]->next=head[headnode2->triple.col]->next->down=last;
headnode2=headnode2->右侧;
if(头节点2==cur2){
cur2=cur2->next;
headnode2=cur2->right;
}
}
else if(headnode1->triple.coltriple.col){
last=last->right=新矩阵节点(false,&headnode1->triple);
head[headnode1->triple.col]->next=head[headnode1->triple.col]->next->down=last;
headnode1=headnode1->右侧;
if(头节点1==cur1){
cur1=cur1->next;
headnode1=cur1->右侧;
}
}
}
}
最后->右=头部[当前行];
对于(int i=0;不精确->向下=头部[i];
对于(整数i=0;整数i=head[i+1];
head[p-1]->next=add.headnode;
添加.headnode->right=head[0];
删除[]标题;
返回添加;
}
istream和运算符>>(istream和is、矩阵和矩阵)
{
三s;
cout s.col>>s.val;
INTP=矩阵最大值(s行,s列);
matrix.headnode=新的Matrixnode(false,&s);
如果(p==0)
{
matrix.headnode->right=matrix.headnode;
回报是;
}
Matrixnode**head=新Matrixnode*[p];
对于(int i=0;it.row>>t.col>>t.val;
如果(t.row>currentRow)
{
最后->右=头部[当前行];
currentRow=t.row;
last=头[当前行];
}
last=last->right=newmatrixnode(false,&t);
头部[t.col]->next=头部[t.col]->next->down=最后一个;
}
最后->右=头部[当前行];
对于(int i=0;不精确->向下=头部[i];
对于(整数i=0;整数i=head[i+1];
head[p-1]->next=matrix.headnode;
矩阵.头节点->右=头[0];
删除[]标题;
回报是;
}
ostream&operatorright;cur!=headnode;cur=cur->right){
操作系统
#include <iostream>
using namespace::std;
struct Triple{
int row;
int col;
int val;
};
class Matrix;
class Matrixnode{
friend class Matrix;
friend istream &operator>>(istream &is,Matrix &matrix);
friend ostream &operator<<(ostream &os,Matrix &matrix);
private:
Matrixnode *right;
Matrixnode *down;
bool head;
union{
Matrixnode* next;
Triple triple;
};
public:
Matrixnode(){
};
Matrixnode(bool b,Triple*t){
head=b;
if(head){right=down=next=this;}
else triple=*t;
};
};
class List{
};
class Matrix{
friend istream& operator>>(istream& is,Matrix& matrix);
friend ostream& operator<<(ostream& os,Matrix& matrix);
public:
Matrix(){
}
int max(int a,int b){
if(a>b)
return a;
else
return b;
}
Matrix Transpose();
Matrix operator+(Matrix &matrix);
private:
Matrixnode* headnode;
};
Matrix Matrix::Transpose(){
Matrixnode *headnode=this->headnode->right;
while(headnode!=this->headnode){
for(Matrixnode *cur=headnode->right;cur!=headnode;cur=cur->right)
swap(cur->triple.row,cur->triple.col);
headnode=headnode->next;
}
return *this;
}
Matrix Matrix::operator+(Matrix &matrix){
Matrixnode *cur1=headnode->right;
Matrixnode *cur2=matrix.headnode->right;
Matrixnode *headnode1=cur1->right;
Matrixnode *headnode2=cur2->right;
Matrix add;
Triple s;
cin >> s.row >> s.col;
s.val=10;
int p=max(s.row,s.col);
int x=matrix.headnode->triple.val;
int y=headnode->triple.val;
add.headnode =new Matrixnode(false,&s);
if(p==0)
{
add.headnode->right=add.headnode;
return add;
}
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<x+y;i++)
{
while(cur1!=this->headnode && cur2!=matrix.headnode){
if (headnode1->triple.row>currentRow && headnode2->triple.row>currentRow)
{
last->right=head[currentRow];
if(headnode1->triple.row>headnode2->triple.row){
currentRow =headnode1->triple.row;
last=head[currentRow];
}
else{
currentRow =headnode2->triple.row;
last=head[currentRow];
}
}
if(headnode1->triple.col==headnode2->triple.col && headnode1->triple.row==headnode2->triple.row){
Triple t;
t.val=headnode1->triple.val+headnode2->triple.val;
t.col=headnode1->triple.col;
t.row=headnode1->triple.row;
last=last->right=new Matrixnode(false,&t);
head[t.col]->next=head[t.col]->next->down=last;
headnode1=headnode1->right;
headnode2=headnode2->right;
if(headnode1==cur1 && cur1!=headnode){
cur1=cur1->next;
headnode1=cur1->right;
}
if(headnode2==cur2 && cur2!=matrix.headnode){
cur2=cur2->next;
headnode2=cur2->right;
}
}
else if(headnode1->triple.col>headnode2->triple.col){
last=last->right=new Matrixnode(false,&headnode2->triple);
head[headnode2->triple.col]->next=head[headnode2->triple.col]->next->down=last;
headnode2=headnode2->right;
if(headnode2==cur2){
cur2=cur2->next;
headnode2=cur2->right;
}
}
else if(headnode1->triple.col<headnode2->triple.col){
last=last->right=new Matrixnode(false,&headnode1->triple);
head[headnode1->triple.col]->next=head[headnode1->triple.col]->next->down=last;
headnode1=headnode1->right;
if(headnode1==cur1 ){
cur1=cur1->next;
headnode1=cur1->right;
}
}
}
}
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=add.headnode;
add.headnode->right=head[0];
delete[]head;
return add;
}
istream &operator>>(istream &is,Matrix &matrix)
{
Triple s;
cout<<"input(row,col,value)"<<endl;
is >> s.row >> s.col >> s.val;
int p=matrix.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.val;i++)
{
Triple t;
is >>t.row>>t.col>>t.val;
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;
}
ostream &operator<<(ostream &os,Matrix &matrix){
Matrixnode *headnode=matrix.headnode->right;
while(headnode!=matrix.headnode)
{
for(Matrixnode *cur=headnode->right;cur!=headnode;cur=cur->right){
os<<cur->triple.row<<","<<cur->triple.col<<","<<cur->triple.val<<endl;
}
headnode=headnode->next;
}
return os;
}
int main(int argc, const char * argv[]) {
Matrix m,n,o;
cin >> m;
cout << m;
cin >> n;
m=m.operator+(n);
cout << m;
}