数据结构-十字链表稀疏矩阵基本操作代码实现

#include <iostream>
#include <iomanip>
using namespace std;

typedef int ElemType;
typedef struct Node{
	int i, j;//该结点非零元的行列下标
	ElemType e;//该结点非零元的值
	struct Node* right, * down;//该十字链表结点的右指针与下指针
}OLNode,*OLink;//十字链表结点
typedef struct {
	OLink* rhead, * chead;//行(列)链表的头指针数组
	int mu, nu, tu;//十字链表的行数、列数、非零元个数
}CrossList;//十字链表

bool InitTSMatrix(CrossList& M);//初始化十字链表
bool CreateTSMatrix(CrossList& M);//构造十字链表
bool TraverseTSMatrix(CrossList M);//遍历十字链表稀疏矩阵
bool SearchTSMatrix(CrossList M, ElemType e);//搜索稀疏矩阵中值为e的非0元素

bool InitTSMatrix(CrossList& M) {
	cout << "输入矩阵行数、列数:";
	cin >> M.mu >> M.nu;
	M.tu = 0;
	M.rhead = new OLink[M.mu + 1]; if (!M.rhead)return 0;//下标从1开始
	M.chead = new OLink[M.nu + 1]; if (!M.chead)return 0;
	for (int i = 1; i <= M.mu; i++)
		M.rhead[i] = NULL;
	for (int i = 1; i <= M.nu; i++)
		M.chead[i] = NULL;
	return 1;
}
bool CreateTSMatrix(CrossList& M) {
	cout << "输入i j e(输入0表示结束): " << endl;
	while (1) {
		int i, j, e;
		cin >> i;
		if (i == 0)break;
		cin >> j >> e;
		OLink temp = new OLNode;
		if (!temp)return 0;
		temp->i = i;
		temp->j = j;
		temp->e = e;
		//插入行链表
		if (M.rhead[i] == NULL || M.rhead[i]->j > j) {
			temp->right = M.rhead[i];
			M.rhead[i] = temp;
		}
		else {
			OLink p = M.rhead[i];
			while (p->right && p->right->j < j)
				p = p->right;
			temp->right = p->right;
			p->right = temp;
		}
		//插入列链表
		if (M.chead[j] == NULL || M.chead[j]->i > i) {
			temp->down = M.chead[j];
			M.chead[j] = temp;
		}
		else {
			OLink p = M.chead[j];
			while (p->down && p->down->i < i)
				p = p->down;
			temp->down = p->down;
			p->down = temp;
		}
		M.tu++;
	}
	return 1;
}
bool TraverseTSMatrix(CrossList M) {
	if (M.tu == 0)return 0;
	for (int i = 1; i <= M.mu; i++) {
		OLink temp = M.rhead[i];
		while (temp) {
			cout << setw(4) << temp->i;
			cout << setw(4) << temp->j;
			cout << setw(6) << temp->e << endl;
			temp = temp->right;
		}
	}
	return 1;
}
bool SearchTSMatrix(CrossList M, ElemType e) {
	int flag = 0;
	for (int i = 1; i <= M.mu; i++) {
		OLink temp = M.rhead[i];
		while (temp) {
			if (temp->e == e) {
				cout << "i=" << temp->i;
				cout << ", j=" << temp->j;
				cout << ", e=" << temp->e << endl;
				flag = 1;
			}
			temp = temp->right;
		}
	}
	
	if (flag)
		return 1;
	return 0;
}


int main()
{
	CrossList M;
	InitTSMatrix(M);
	if (!CreateTSMatrix(M)) {
		cout << "稀疏矩阵十字链表创建失败!" << endl;
		return 1;
	}
	cout << "稀疏矩阵为:\n";
	cout << "---------------\n";
	cout << "   i   j     e\n";
	cout << "---------------\n";
	if (!TraverseTSMatrix(M)) {
		cout << "该矩阵没有非零元素!" << endl;
		return 2;
	}
	cout << "---------------" << endl;
	ElemType e;
	cout << "输入要查找的矩阵元素:\n";
	cin >> e;
	cout << "查询情况如下:\n";
	if (!SearchTSMatrix(M, e)) {
		cout << "没有找到该元素!" << endl;
	}
	return 0;
}

实验样例:

 

 

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值