IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 数据结构稀疏矩阵三元组的创建与插入 -> 正文阅读

[数据结构与算法]数据结构稀疏矩阵三元组的创建与插入

#include<iostream>
#define MaxSize 100
using namespace std;
typedef struct
{
	int row;//the row of the element(该元素所在的行)
	int column;//the column in  which the element is located(该元素所在的列)
	int element;
}TupNode;
class Triolet
{
private:
	TupNode tuple[MaxSize];
	int NR;//the number of rows in a triple(三元组所含的行数)
	int NC;//the number of columns in a triple(三元组所含的列数)
	int num;//number of triple element(三元组元素个数)
public:
	Triolet()
	{
		NC = 0;
		NR = 0;
		num = 0;
	}
	void CreatMat()
	{
		cout << "输入矩阵的行和列" << endl;
		cin >> NR >> NC;
		cout << "输入不为0的元素个数" << endl;
		cin >> num;
		for (int i = 1; i <= num; ++i)
		{
			cout << "输入第" << i << "个元素" << endl;
			cin >> tuple[i].element;
			//prevent position elements from crossing boundaries(防止输入元素越界)
			cout << "输入第" << i << "个元素所在的行" << endl;
			while (1)
			{
				cin >> tuple[i].row;
				if (tuple[i].row <= NR && tuple[i].row > 0)
					break;
				else
					cout << "该元素行位置输入越界,请重新输入" << endl;
			}
			cout << "输入第" << i << "个元素所在的列" << endl;
			while (1)
			{
				cin >> tuple[i].column;
				if (tuple[i].column <= NC && tuple[i].column > 0)
					break;
				else
					cout << "该元素列位置输入越界请重新输入" << endl;
			}
		}
		SortTriolet();

	}
	void SortTriolet()//sort triplesby roes and columns of the matrix(按矩阵的行与列,对三元组进行排序)
	{

		if (num == 1)return;
		//sort rows and columns from small to large(按行列从小到大排序)
		//shell sort(希尔排序)
		int d = num / 2;
		for (; d > 0; d = d / 2)
			for (int i = d + 1; i <= num; ++i)
			{
				tuple[0] = tuple[i];
				int j;
				for (j = i - d; j > 0 && tuple[j].row > tuple[0].row && tuple[j].column > tuple[0].row; tuple[j + d] = tuple[j], j = j - d);
				tuple[j + d] = tuple[0];

			}
		CheckDuplicate();//delete elements with duplicate rows and colums(删去行列重复的元素)
	}
	void CheckDuplicate()
	{
		//delete elements with duplicate rows and colums(删去行列重复的元素)
		int j = 0;
		for (int i = 1; i <= num; ++i)
		{
			if (tuple[i].row != tuple[i + 1].row && tuple[i].column != tuple[i + 1].column)
			{
				++j;
				tuple[j] = tuple[i];
				

			}
		}
		num = j;
	}

	void InsertElement()
	{
		
		cout << "输入插入的元素" << endl;
		cin >> tuple[0].element;
		cout << "输入该元素所在的行" << endl;
		while (1)
		{
			cin >> tuple[0].row;
			if (tuple[0].row > 0 && tuple[0].row <= NR)
				break;
			else
				cout << "行坐标输入有误,请重新输入" << endl;
		}
		cout << "输入该元素所在的列" << endl;
		while (1)
		{
			cin >> tuple[0].column;
			if (tuple[0].column > 0 && tuple[0].column <= NC)
				break;
			else
				cout << "列坐标输入有误,请重新输入" << endl;
		}

		for (int i = num; i > 0; --i)
		{
			if (tuple[i].row > tuple[0].row)
				tuple[i + 1] = tuple[i];
			else if (tuple[i].column > tuple[0].column)
				tuple[i + 1] = tuple[i];
			else if (tuple[i].row == tuple[0].row && tuple[i].column == tuple[0].column)
			{
				tuple[i].element = tuple[0].element;
				break;
			}
			else
			{
				tuple[i + 1] = tuple[0];
				++num;
				break;
			}
		}
		cout << "插入完成" << endl;
		OutputMatrix();
	}
	void OutputMatrix()
	{
		cout << "矩阵" << endl;
		int index = 1;
		for (int i = 1; i <=NR; ++i)
		{
			for (int j = 1; j <=NC; ++j)
			{
				if (i == tuple[index].row && j == tuple[index].column&&index<=num)
				{
					cout << tuple[index].element << '\t';
					++index;
				}
				else cout << "0" << '\t';

			}
			cout << endl;
			
		}
	}
	void OutputTriolet()
	{
		cout << "三元组" << endl;
		for (int i = 1; i <= num; ++i)
		{
			cout << tuple[i].element << " 在第 " << tuple[i].row << " 行,第 " <<tuple[i].column<< "列" << endl;
		}
	}
};
int main()
{
	Triolet triolet;
	triolet.CreatMat();
	triolet.OutputMatrix();
	triolet.OutputTriolet();
	triolet.InsertElement();
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 11:02:29  更:2021-08-02 11:05:16 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 12:46:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码