| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 506 人关注过本帖
标题:[求助]江湖救急!哪个大哥近来看看!
只看楼主 加入收藏
int1314
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-1-11
收藏
 问题点数:0 回复次数:0 
[求助]江湖救急!哪个大哥近来看看!

这是1段 C++ 实现的 十字连表存储稀疏矩阵 的算法! 由于小弟C ++ 才刚学习!看不太懂
能否请求 高手帮我 改为 C 实现的 “邻接表存储结构 来实现稀疏矩阵”? 拜谢!小弟 1晚都在线!



#ifndef Matrix_H

#define Matrix_H

#include "List.h"

class MatNode

{

public:

int data;

int row, col;

union { Node<MatNode> *down; List<MatNode> *downrow; };

MatNode(int value = 0, Node<MatNode> *p = NULL, int i = 0, int j = 0)

: data(value), down(p), row(i), col(j) {}

friend ostream & operator << (ostream & strm, MatNode &mtn)

{

strm << '(' << mtn.row << ',' << mtn.col << ')' << mtn.data;

return strm;

}

};

class Matrix : List<MatNode>

{

public:

Matrix() : row(0), col(0), num(0) {}

Matrix(int row, int col, int num) : row(row), col(col), num(num) {}

~Matrix() { MakeEmpty(); }

void MakeEmpty()

{

List<MatNode> *q;

while (first->data.downrow != NULL)

{

q = first->data.downrow;

first->data.downrow = q->first->data.downrow;

delete q;

}

List<MatNode>::MakeEmpty();

row = col = num = 0;

}

void Input()

{

if (!row) { cout << "输入矩阵行数:"; cin >> row; }

if (!col) { cout << "输入矩阵列数:"; cin >> col; }

if (!num) { cout << "输入非零个数:"; cin >> num; }

if (!row || !col || !num) return;

cout << endl << "请按顺序输入各个非零元素,以列序为主,输入0表示本列结束" << endl;

int i, j, k, v;//i行数 j列数 k个非零元 v非零值

Node<MatNode> *p = first, *t;

List<MatNode> *q;

for (j = 1; j <= col; j++) LastInsert(MatNode(0, NULL, 0, j));

for (i = 1; i <= row; i++)

{

q = new List<MatNode>;

q->first->data.row = i;

p->data.downrow = q;

p = q->first;

}

j = 1; q = first->data.downrow; First(); t = pNext();

for (k = 0; k < num; k++)

{

if (j > col) break;

cout << endl << "输入第" << j << "列非零元素" << endl;

cout << "行数:"; cin >> i;

if (i < 1 || i > row) { j++; k--; q = first->data.downrow; t = pNext(); continue; }

cout << "非零元素值"; cin >> v;

if (!v) { k--; continue; }

MatNode matnode(v, NULL, i, j);

p = new Node<MatNode>(matnode);

t->data.down = p; t = p;

while (q->first->data.row != i) q = q->first->data.downrow;

q->LastInsert(t);

}

}

void Print()

{

List<MatNode> *q = first->data.downrow;

cout << endl;

while (q != NULL)

{

cout << *q;

q = q->first->data.downrow;

}

}

Matrix & Add(Matrix &matB)

{

//初始化赋值辅助变量

if (row != matB.row || col != matB.col || matB.num == 0) return *this;

Node<MatNode> *pA, *pB;

Node<MatNode> **pAT = new Node<MatNode>*[col + 1];

Node<MatNode> **pBT = new Node<MatNode>*[matB.col + 1];

List<MatNode> *qA = pGetFirst()->data.downrow, *qB = matB.pGetFirst()->data.downrow;

First(); matB.First();

for (int j = 1; j <= col; j++)

{

pAT[j] = pNext();

pBT[j] = matB.pNext();

}

//开始

for (int i = 1; i <= row; i++)

{

qA->First(); qB->First();

pA = qA->pNext(); pB = qB->pNext();

while (pA != NULL && pB !=NULL)

{

if (pA->data.col == pB->data.col)

{

pA->data.data += pB->data.data;

pBT[pB->data.col]->data.down = pB->data.down; qB->Remove();

if (!pA->data.data)

{

pAT[pA->data.col]->data.down = pA->data.down;

qA->Remove();

}

else

{

pAT[pA->data.col] = pA;

qA->pNext();

}

}

else

{

if (pA->data.col > pB->data.col)

{

pBT[pB->data.col]->data.down = pB->data.down;

qB->pRemove();

pB->data.down = pAT[pB->data.col]->data.down;

pAT[pB->data.col]->data.down = pB;

pAT[pB->data.col] = pB;

qA->InsertBefore(pB);

}

else if (pA->data.col < pB->data.col)

{

pAT[pA->data.col] = pA;

qA->pNext();

}

}

pA = qA->pGet();pB = qB->pGet();

}

if (pA == NULL && pB != NULL)

{

qA->pGetPrior()->link = pB;

qB->pGetPrior()->link = NULL;

while (pB != NULL)

{

pBT[pB->data.col]->data.down = pB->data.down;

pB->data.down = pAT[pB->data.col]->data.down;

pAT[pB->data.col]->data.down = pB;

pAT[pB->data.col] = pB;

pB = pB->link;

}

}

if (pA !=NULL)

{

while (qA->pGet() != NULL)

{

pAT[pA->data.col] = pA;

qA->pNext();

}

}

qA = qA->first->data.downrow; qB = qB->first->data.downrow;

}

delete []pAT; delete []pBT;
return *this;

}

private:

int row, col, num;

};

[此贴子已经被作者于2006-1-11 1:33:52编辑过]

搜索更多相关主题的帖子: 江湖 
2006-01-11 01:33
快速回复:[求助]江湖救急!哪个大哥近来看看!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029733 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved