C++实现稀疏矩阵的十字链表表示法
来源:互联网 发布:excel只显示重复数据 编辑:程序博客网 时间:2024/05/17 03:44
/* * OLMat.h * * Created on: Oct 17, 2015 * Author: chris */#ifndef OLMAT_H_#define OLMAT_H_#include<iostream>typedef int ElemType;struct OLNode{int i, j;ElemType e;OLNode *right, *down;OLNode():i(0), j(0),right(NULL), down(NULL) {}};struct OLMat{OLNode *rhead, *chead;int mu, nu, tu;OLMat(): rhead(NULL), chead(NULL),mu(0), nu(0), tu(0) {}};bool OLMatCreate(OLMat & mat, int nrows, int ncols);void OLMatDestroy(OLMat & mat);bool OLMatPutVal(OLMat & mat, int i, int j, ElemType e);bool OLMatGetVal(OLMat & mat, int i, int j, ElemType& e);bool OLMatDelEnt(OLMat & mat, int i, int j);bool OLMatInput(OLMat & mat);void OLMatPrint(OLMat & mat);void OLMatTranspose(OLMat & mat);#endif /* OLMAT_H_ */
/* * OLMat.cpp * * Created on: Oct 17, 2015 * Author: chris */#include"OLMat.h"#include<iostream>using namespace std;bool OLMatCreate(OLMat & mat, int nrows, int ncols){mat.rhead = new OLNode[nrows];if(!mat.rhead) return false;mat.chead = new OLNode[ncols];if(!mat.chead) { delete mat.rhead;return false;}mat.mu = nrows;mat.nu = ncols;mat.tu = 0;return true;}void OLMatDestroy(OLMat & mat){for(int i = 0; i < mat.mu; ++i) {OLNode * rprior = mat.rhead+i;while(rprior->right) {OLNode * pcur = rprior->right;rprior->right = pcur->right;delete pcur;}//endw}//endfdelete mat.rhead;delete mat.chead;mat.mu = mat.nu = mat.tu = 0;}bool OLMatPutVal(OLMat & mat, int i, int j, ElemType e){if(i < 0 || i >= mat.mu || j < 0 || j >= mat.nu) {cerr << "Index out of bound." << endl;return false;}//find pos.OLNode *rprior = mat.rhead+i;while(rprior->right && rprior->right->j < j)rprior = rprior->right;if(rprior->right && rprior->right->j == j) {rprior->right->e = e;}else{OLNode *pcur = new OLNode;if(!pcur) return false;//ins rowpcur->i = i; pcur->j = j; pcur->e = e;pcur->right = rprior->right;rprior->right = pcur;++mat.tu;//ins colOLNode* cprior = mat.chead+j;while(cprior->down && cprior->down->i < i)cprior = cprior->down;pcur->down = cprior->down;cprior->down = pcur;} //endifreturn true;}bool OLMatGetVal(OLMat & mat, int i, int j, ElemType& e){if(i < 0 || i >= mat.mu || j < 0 || j >= mat.nu) {cerr << "Index out of bound." << endl;return false;}e = 0; // default value.OLNode *pcur = mat.rhead[i].right;while(pcur && pcur->j < j)pcur = pcur->right;if(pcur && pcur->j == j)e = pcur->e; return true;}bool OLMatDelEnt(OLMat & mat, int i, int j){if(i < 0 || i >= mat.mu || j < 0 || j >= mat.nu)return false;OLNode * rprior = mat.rhead+i;while(rprior->right && rprior->right->j < j)rprior = rprior->right;if(rprior->right && rprior->right->j == j) {OLNode * pcur = rprior->right;rprior->right = pcur->right;OLNode * cprior = mat.chead + j;while(cprior->down != pcur)cprior = cprior->down;cprior->down = pcur->down;delete pcur;--mat.tu;return true;}return false;}bool OLMatInput(OLMat & mat){cout << "Input a "<< mat.mu << " x " << mat.nu<< " matrix: " << endl;int n;cout << "Number of nonzeros: ";cin >> n;cout << "Input row, col, val: " << endl;int row, col;ElemType e;for(int i = 0; i < n; ++i) {cin >> row >> col >> e;if(!OLMatPutVal(mat, row, col, e))return false;}return true;}void OLMatPrint(OLMat & mat){for(int i = 0; i < mat.mu; ++i) {OLNode * pcur = mat.rhead[i].right;int nextj = 0;while(pcur) {while(nextj != pcur->j) {cout << "0 ";++nextj;} //endwcout << pcur->e << " ";pcur = pcur->right;++nextj;}//endwwhile(nextj != mat.nu) {cout << "0 ";++nextj;}cout << endl;} // endf}void OLMatTranspose(OLMat & mat){for(int i = 0; i < mat.mu; ++i) {OLNode* rprior = mat.rhead+i;while(rprior) {OLNode * rnext = rprior->right;//swap i, jint t = rprior->i;rprior->i = rprior->j;rprior->j = t;//swap right, downOLNode * tp = rprior->right;rprior->right = rprior->down;rprior->down = tp;rprior = rnext;}}//endffor(int j = 0; j < mat.nu; ++j)mat.chead[j].right = mat.chead[j].down;int t = mat.mu;mat.mu = mat.nu;mat.nu = t;OLNode* tp = mat.rhead;mat.rhead = mat.chead;mat.chead = tp;}
/* * Main.cpp * * Created on: Oct 12, 2015 * Author: chris */#include<iostream>#include"OLMat.h"using namespace std;int main(void){OLMat mat;int r, c;cout << "nrows, ncols: " << endl;cin >> r >> c;OLMatCreate(mat, r, c);int n;cout << "num of nonzeros: " << endl;cin >> n;cout << "i, j, e: " << endl;for(int i = 0; i < n; ++i) {ElemType e;cin >> r >> c >> e;if(!OLMatPutVal(mat, r, c, e))cout << "input failed." << endl;}//endfOLMatPrint(mat);cout << endl;OLMatTranspose(mat);OLMatPrint(mat);cout << endl;OLMatDestroy(mat);system("pause");return 0;}
0 0
- C++实现稀疏矩阵的十字链表表示法
- 线性代数4:稀疏矩阵的十字链表表示法
- 稀疏矩阵十字链表表示
- 十字链表表示稀疏矩阵的基本操作
- 用十字链表表示的稀疏矩阵类
- 稀疏矩阵的十字链表表示法及其加法减法运算算法
- 数据结构例程——稀疏矩阵的十字链表表示
- 第八周实践项目10 稀疏矩阵的十字链表表示
- 数据结构——稀疏矩阵的十字链表表示方法
- 以三元组形式输出用十字链表表示的稀疏矩阵中非零元素及其下标的算法
- 数据结构-十字链表表示矩阵(C++)
- 稀疏矩阵的十字链表实现(C语言实现)
- 一个用十字链表表示的矩阵
- 数据结构之---C/C++实现稀疏矩阵的十字链表
- 数据结构C语言实现稀疏矩阵的压缩和运算的三元组顺序表表示法
- 稀疏矩阵的十字链表实现:行列链表
- 十字链表 稀疏矩阵的数据结构
- 稀疏矩阵的十字链表存储
- Hadoop集群虚拟机网卡的介绍和配置
- ios9 qq微信微博等分享用不了的解决方案
- UDP TCP区别
- 安装Docker
- 转义符
- C++实现稀疏矩阵的十字链表表示法
- Git 命令详解
- Android 传感器 (详解二)—— 常用传感器
- Java Portlet 规范概述
- 滴滴快的_2016_面试题_笔试题_产品经理_题来了
- ALM工具使用流程(一)
- Android-jni开发
- 有符号和无符号 unsigned signed
- 滴滴快的_2016_面试题_笔试题_产品经理_题来了