求教:怎么用Linked List来实现两组sparse matrix的相加?

NZGreg 2009-03-03 09:17:01
Question

Reads two sparse matrices and stores them as linked list,
then add the two sparse matrices together, produce the result matrix that be stored in linked list as well.
And display all three matrices in the usual matrix format.


Thanks!!!!!!!!!
...全文
398 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzy340623339 2009-03-04
  • 打赏
  • 举报
回复
思想就是:
1、把两个matric的非零数据、行下标、列下标分别存入两个链表中;
2、遍历矩阵,同时用p,q指针遍历两个链表,如果
  (1)p,q非空,且p、q的行列下标值与矩阵下标一致,则有元素值为p->data + q->data;
  (2)p,q非空,且p的行列下标值与矩阵下标一致,但q的行列下标值与矩阵下标不一致,则有元素值为p->data;
  (3)p,q非空,且q的行列下标值与矩阵下标一致,但p的行列下标值与矩阵下标不一致,则有元素值为q->data;
  (4)p空且q非空,且q的行列下标值与矩阵下标一致,则有元素值为q->data;
  (5)p非空且q空,且p的行列下标值与矩阵下标一致,则有元素值为p->data;
这样就实现了两个矩阵相加,只不过现在的结果是在链表中
3、遍历矩阵,同时用p指针遍历结果链表,
(1)p不空,且p的行列下标变量的值与矩阵下标一致,则输出p->data;
(2)如果p的行列下标变量的值与矩阵下标不一致,则输出0.0;
(3)输出一行后换行。
4、释放内存空间。


NZGreg 2009-03-04
  • 打赏
  • 举报
回复
谢了 这回没问题了!
NZGreg 2009-03-04
  • 打赏
  • 举报
回复
谢谢3楼的朋友,
不过result 好像有问题~
lzy340623339 2009-03-04
  • 打赏
  • 举报
回复
在我的电脑上,vs 2003中的运行结果
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
Press any key to continue
lzy340623339 2009-03-04
  • 打赏
  • 举报
回复
调试好了的
#include "stdafx.h" 
#include <iostream>
using namespace std;

#define N 5

typedef struct node{
unsigned int row;
unsigned int column;
double data;
struct node *next;
}node,*pnode;

void mattolin(pnode &l,const double (*m)[N]) /*matric to linked list*/
{
if(l != NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
pnode p;
int i,j;
l = new node;
l->next=NULL;
p=l;
for( i= 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if((*m)[i*N+j] != 0.0)
{
p->next = new node;
p->next->row = i;
p->next->column = j;
p->next->data = (*m)[i*N+j];
p->next->next = NULL;
p=p->next;
}
}
}
}

void Add2mat(const pnode &l1,const pnode &l2,pnode &l3) /*add the two matrices to linked list 3*/
{
if(l1 == NULL || l2 == NULL || l3 !=NULL)
{
exit(0);
}
int i,j;
pnode p,q,t;
l3 = new node;
l3->next=NULL;
t = l3;
p = l1->next;
q = l2->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL && q !=NULL)
{
if(p && q && p->row == i && p->column == j && q->row == i && q->column == j)
{
t ->next= new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data + q->data;
t = t->next;
p = p->next;
q = q->next;
}
if(p && q && (p->row != i || p->column != j) && (q->row == i && q->column == j))
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t = t->next;
q = q->next;
}
if(p && q && (p->row == i && p->column == j) && (q->row != i || q->column != j))
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
else if(p != NULL)
{
if(p->row == i && p->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
else if(q != NULL)
{
if( q->row == i && q->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t = t->next;
q = q->next;
}
}
}
}
}

void printmatric(const pnode &l) /*print the result matric*/
{
if( l == NULL)
{
cout<<"Linked list is NULL"<<endl;
exit(0);
}
int i,j;
pnode p;
p = l->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL)
{
if(p->row == i && p->column == j)
{
cout << p->data<< ' ';
p = p->next;
continue;
}
}
cout<<"0.0"<<' ';
}
cout<<endl;
}
}

void destroyList(pnode &l)
{
if( l == NULL)
return;
pnode p = l->next;
while(p)
{
l->next = p->next;
delete p;
p = l->next;
}
delete l;
}

void main()
{
double matric1[N][N] = {{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5}};
double matric2[N][N] = {{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9}};
pnode l1 = NULL,l2 = NULL,l3 = NULL;
mattolin(l1,matric1);
mattolin(l2,matric2);
Add2mat(l1,l2,l3);
printmatric(l3);
destroyList(l1);
destroyList(l2);
destroyList(l3);
}
NZGreg 2009-03-04
  • 打赏
  • 举报
回复
太感谢楼上的朋友了, 受益匪浅!!!
grellen 2009-03-04
  • 打赏
  • 举报
回复
随便写了下
#include<iostream>
using namespace std;
template<typename T>
class Node
{
public:
T data;
Node* next;
};
template<typename T, int LEN>
class Matrix
{
private:
Node<T>* head[LEN];
public:
Matrix()
{

for (int i = 0; i < LEN; i++)
{
head[i] = NULL;
}
}
Matrix(const Matrix<T,LEN>& rhs)
{
for (int i = 0; i < LEN; i++)
{
if ( rhs.head[i] == NULL )
return;
}

for (int j = 0; j < LEN; j++)
{
Node<T>* s;
s = rhs.head[j];
Node<T>* current = new Node<T>;
current -> data = s-> data;
s = s -> next;
head[j] = current;
while ( s != NULL )
{
Node<T>* nex = new Node<T>;
current -> next = nex;
nex -> data = s->data;
current = nex;
s = s -> next;
}
current -> next = NULL;
}
}


void operator = (const Matrix<T,LEN>& rhs)
{
for (int i = 0; i < LEN; i++)
{
if ( rhs.head[i] == NULL )
return;
}

for (int j = 0; j < LEN; j++)
{
Node<T>* s;
s = rhs.head[j];
Node<T>* current = new Node<T>;
current -> data = s-> data;
s = s -> next;
head[j] = current;
while ( s != NULL )
{
Node<T>* nex = new Node<T>;
current -> next = nex;
nex -> data = s->data;
current = nex;
s = s -> next;
}
current -> next = NULL;
}
}

Matrix operator+(const Matrix& rhs)
{
Matrix* result = new Matrix();

for (int i = 0; i < LEN; i++)
{
Node<T>* right = rhs.head[i];
Node<T>* left = this->head[i];
for (int j = 0; j < LEN; j++)
{
Node<T>* newp = new Node<T>;
newp -> next = NULL;
newp->data = right->data + left->data;
right = right->next;
left = left->next;
if ( result->head[i] == NULL )
{
newp -> next = result->head[i];
result->head[i] = newp;
}
else
{
Node<T>* q = result->head[i];
while ( q -> next != NULL )
q = q -> next;
q -> next = newp;
}
}
}
return *result;

}

friend istream& operator>> (istream& ins, Matrix<T,LEN>& obj)
{

for (int i = 0; i < LEN; i++)
{
for (int j = 0; j < LEN; j++)
{
Node<T>* newp = new Node<T>;
ins>>newp->data;
newp -> next = NULL;
if ( obj.head[i] == NULL )
{
newp -> next = obj.head[i];
obj.head[i] = newp;
}
else
{
Node<T>* q = obj.head[i];
while ( q -> next != NULL )
q = q -> next;
q -> next = newp;
}
}


}
return ins;
}

friend ostream& operator << (ostream& out, Matrix<T, LEN>& obj)
{

for (int i = 0; i < LEN; i++)
{
Node<T>* temp = obj.head[i];
for (int j = 0; j < LEN; j++)
{
out<<temp->data<<" ";
temp = temp->next;
}
out<<endl;
}
return out;
}
~Matrix()
{
for (int i = 0; i < LEN; i++)
{
Node<T> *p = head[i], *q;
while ( p != NULL )
{
q = p -> next;
delete p;
p = q;
}
head[i] = NULL;

}

}
};

int main()
{

Matrix<int,5> a;
Matrix<int,5> b;
Matrix<int,5> c; //输入一个五行五列的矩阵
cout<<"请输入第一个矩阵"<<endl;
cin>>a;
cout<<"第一个矩阵为:"<<endl<<a;
cout<<"请输入第二个矩阵"<<endl;
cin>>b;
cout<<"第二个矩阵为:"<<endl<<b;

c = a + b;

cout<<"两矩阵相加的结果为:"<<endl<<c;

return 0;
}
  • 打赏
  • 举报
回复
用matlab吧,大哥.
lzy340623339 2009-03-03
  • 打赏
  • 举报
回复
终于写得,没调试过,呵呵
#include <iostream>
using namespace std;

#define N 5

typedef struct node{
unsigned int row;
unsigned int column;
double data;
struct node *next;
}node,*pnode;

void mattolin(pnode &l,const double (*m)[N]) /*matric to linked list*/
{
if(l != NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
pnode p;
int i,j;
l = new node;
p=l;
for( i= 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if((*m)[i*N+j] != 0.0)
{
p->next = new node;
p->next->row = i;
p->next->column = j;
p->next->data = (*m)[i*N+j];
p->next->next = NULL;
p=p->next;
}
}
}
}

void Add2mat(const pnode &l1,const pnode &l2,pnode &l3) /*add the two matrices to linked list 3*/
{
if(l1 == NULL || l2 == NULL || l3 !=NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
int i,j;
pnode p,q,t;
l3 = new node;
t = l3;
p = l1->next;
q = l2->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p->row == i && p->column == j && q->row == i && q->column == j)
{
t ->next= new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data + q->data;
p = p->next;
q = q->next;
}
if(p->row != i && p->column != j && q->row == i && q->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t=t->next;
q = q->next;
}
if(p->row == i && p->column == j && q->row != i && q->column != j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
}
}

void printmatric(const pnode &l) /*print the result matric*/
{
if( l == NULL)
{
cout<<"Linked list is NULL"<<endl;
exit(0);
}
int i,j;
pnode p;
p = l->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL)
{
if(p->row == i && p->column == j)
{
cout << p->data<< ' ';
p = p->next;
continue;
}
}
cout<<"0.0"<<' ';
}
cout<<endl;
}
}

void destroyList(pnode l)
{
if( l == NULL)
return;
pnode p = l->next;
while(p)
{
l->next = p->next;
delete p;
}
delete l;
}

void main()
{
double matric1[N][N] = {{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5}};
double matric2[N][N] = {{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9}};
pnode l1 = NULL,l2 = NULL,l3 = NULL;
mattolin(l1,matric1);
mattolin(l2,matric2);
printmatric(l1);
Add2mat(l1,l2,l3);
printmatric(l3);
destroyList(l1);
destroyList(l2);
destroyList(l3);
}
grellen 2009-03-03
  • 打赏
  • 举报
回复
up
lzy340623339 2009-03-03
  • 打赏
  • 举报
回复
mark

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧