Titan笔记

  • 首页
  • Java
  • 数据结构
  • Web
  • C语言
  • Python
  • 杂谈
  • 逸笔挥墨
Titan笔记
分享学习、研究与开发过程中的点滴记忆
  1. 首页
  2. 数据结构
  3. 正文

[数据结构] 稀疏矩阵的存储

2020年4月4日 251点热度 4人点赞 0条评论

【问题描述】

稀疏矩阵是指那些多数元素为零的矩阵。利用“稀疏”特点进行存储和计算可以大大节省存储空间,提高计算效率。实现一个能进行稀疏矩阵基本运算的运算器。

【基本要求】

以三元组顺序表表示稀疏矩阵,实现两个矩阵相加、相减的运算。稀疏矩阵的输入形式采用三元组表示,而运算结果的矩阵则以通常的阵列形式列出。

稀疏矩阵加减法例子

【Talk is cheap, show you the code】

#include<iostream>
// By Titan 2020-03-30
using namespace std;
typedef struct TupNode TUP;
typedef struct Matrix *TSM;

struct TupNode {
	int col;
	int row;
	int data;
};
struct Matrix {
	int cols;
	int rows;
	int nums;
	TUP data[100];
};

TSM inputData(int rows,int cols) {
	int a[rows][cols];
	for(int i=0; i<rows; i++) {
		for(int j=0; j<cols; j++) {
			cin>>a[i][j];
		}
	}
	TSM T=new Matrix;
	T->rows=rows;
	T->cols=cols;
	T->nums=0;
	for(int i=0; i<rows; i++) {
		for(int j=0; j<cols; j++) {
			if(a[i][j]!=0) {
				T->data[T->nums].data=a[i][j];
				T->data[T->nums].row=i;
				T->data[T->nums].col=j;
				T->nums++;
			}
		}
	}
	return T;
}

void printTSM(TSM T) {
	if(!T) {
		cout<<"Invalid TSMatrix!"<<endl;
		return;
	} else if(T->nums<=0) {
		cout<<"Invalid TSMatrix!"<<endl;
		return;
	}
	cout<< "Total Rows:" << T->rows << endl<<"Total Columns: " << T->cols << endl;
	cout<< "Non-Zero Elements Count: "<<T->nums<<endl;
	cout<<"--------------------------------------------------------"<<endl;
	cout<<"Row\tColumn\tData"<<endl;
	for(int i=0; i<T->nums; i++) {
		cout<<T->data[i].row
		    <<"\t"<<T->data[i].col
		    <<"\t"<<T->data[i].data
		    <<endl;
	}
}
void printAsArray(TSM T){
	if(!T){
		cout<<"Invalid TSMatrix!"<<endl;
		return;
	}
	int a,b,col,row,data;
	a=T->cols;
	b=T->rows;
	int arr[a][b];
	for(int i=0;i<a;i++){
		for(int j=0;j<b;j++){
			arr[i][j]=0;
		}
	}
	for(int i=0;i<T->nums;i++){
		col=T->data[i].col;
		row=T->data[i].row;
		data=T->data[i].data;
		arr[row][col]=data;
	}
	for(int i=0;i<b;i++){
		for(int j=0;j<a;j++){
			cout<<arr[i][j]<<"\t";
		}
		cout<<endl;
	}
}
void insertTSM(TSM T,int data,int row,int col){
	if(!T){
		return;
	}
	bool flag=true;
	for(int i=0;i<T->nums;i++){
		if(T->data[i].col==col && T->data[i].row == row){
			T->data[i].data+=data;
			if(T->data[i].data==0){
				for(int j=T->nums-1;j>i;j--){
					T->data[j-1]=T->data[j];
				}
				T->nums--;
			}
			flag=false;
			break;
		}
	}
	if(flag){
		T->data[T->nums].col=col;
		T->data[T->nums].row=row;
		T->data[T->nums].data=data;
		T->nums++;
	}

}
TSM addTSM(TSM T1,TSM T2){
	if(T1->cols!=T2->cols || T1->rows!=T2->rows){
		return NULL;
	}
	TSM NT=new Matrix();
	NT->cols=T1->cols;
	NT->rows=T1->rows;
	NT->nums=0;
	for(int i=0;i<T1->nums;i++){
		insertTSM(NT,T1->data[i].data,T1->data[i].row,T1->data[i].col);
	}
	for(int i=0;i<T2->nums;i++){
		insertTSM(NT,T2->data[i].data,T2->data[i].row,T2->data[i].col);
	}
	return NT;
}

TSM subTSM(TSM T1,TSM T2){
	for(int i=0;i<T2->nums;i++){
		T2->data[i].data=-T2->data[i].data;
	}
	return addTSM(T1,T2);
}

int main() {
	int a,b,choice=-1;
	cout<<"Please input the total rows and columns:"<<endl;
	cin>>a>>b;
	TSM T1=inputData(a,b);
	printTSM(T1);
	cout<<"Please input the total rows and columns:"<<endl;
	cin>>a>>b;
	TSM T2=inputData(a,b);
	printTSM(T2);
	TSM NewT;
	cout<<"Please select the action(0=Addition,1=Subtraction:)"<<endl;
	while(choice !=0 && choice!=1){
		cin>>choice;
		if(choice==0){
			NewT=addTSM(T1,T2);
		}else if(choice==1){
			NewT=subTSM(T1,T2);
		}else{
			cout<<"Invilid Choice!"<<endl;
		}
	}
	cout<<"The New TSMatrix is:"<<endl; 
	printTSM(NewT);
	cout<<"------------------------------------"<<endl;
	cout<<"The New Matrix is:"<<endl; 
	printAsArray(NewT);
}
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2020年4月4日

Titan

兴趣广泛而无一精擅
想到什么,我总是渴望以代码的方式去呈现
永远年轻,永远热泪盈眶
Stay Hungry, Stay Foolish

点赞
< 上一篇
下一篇 >

文章评论

取消回复

Titan

兴趣广泛而无一精擅
想到什么,我总是渴望以代码的方式去呈现
永远年轻,永远热泪盈眶
Stay Hungry, Stay Foolish

逸笔挥墨 - Titan的文学天地
文章分类
  • C语言 (4)
  • Hadoop (1)
  • Hive (3)
  • Java (18)
  • JavaWeb (3)
  • Linux运维之道 (1)
  • Mybatis学习笔记 (3)
  • Python (3)
  • SpringCloud (3)
  • Web (5)
  • Web前端 (4)
  • Web后端 (5)
  • 数据库 (1)
  • 数据结构 (10)
  • 杂谈 (3)
  • 诗词歌赋 (1)
  • 随摘 (2)
最新 热点 随机
最新 热点 随机
Spring Cloud 微服务学习笔记 - Eureka 服务注册与发现 Spring Cloud 微服务学习笔记 - IDEA工程搭建 关于我和Titan笔记 Spring Cloud 微服务学习笔记 - 开篇 TitanEMS - Titan企业员工管理系统 - JavaWeb期末实践项目 Linux 网络优化指南 - 改善Linux的网络性能
Spring Cloud 微服务学习笔记 - 开篇TitanEMS - Titan企业员工管理系统 - JavaWeb期末实践项目2021年1月随摘2021年1月诗摘关于我和Titan笔记《梦之浮桥》中的几句
[Web] CSS 中 Display(显示) 与 Visibility(可见性)的区别与用法 SpringBoot整合JWT认证机制实现接口鉴权 Spring与Mybatis的整合 Java核心技术之动态代理 TitanEMS - Titan企业员工管理系统 - JavaWeb期末实践项目 [DEMO] Titan的WEB期末项目
标签聚合
数据结构 Mybatis学习笔记 Python 二叉树 Apache-Hive 链式存储 JavaWeb Java
友情链接
  • Mttblog

COPYRIGHT © 2016 - 2021 Titan笔记. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

豫ICP备20001822号-1

豫公网安备 41010502004418号