数据结构-十字链表表示矩阵(C++)

本文介绍了一种使用C++实现十字链表来表示稀疏矩阵的方法,包括三元组类、十字链表节点类和十字链表矩阵类的详细定义,以及插入元素、获取元素等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef CCROSS_SPARSE_H
#define CCROSS_SPARSE_H

/************************************************************************/  
/* 以下是C++ 矩阵 类, 十字链表; 
/************************************************************************/  

//三元组类
template<class Elemplent>
class CTriple
{
public:
	int rows,cols;
	CTriple(int r,int c,Elemplent tempelemplent);
	CTriple();
	Elemplent value;
};

//构造函数
template <class Elemplent>
CTriple<Elemplent>::CTriple()
{

}

//构造函数
template<class Elemplent>
CTriple<Elemplent>::CTriple(int r,int c,Elemplent tempelemplent)
{
	rows =r;
	cols =c;
	value = tempelemplent;
}


//三元组节点定义
template<class Elemplent>
class CCrossNode
{
public:
	CTriple<Elemplent> nodectriple;
	CCrossNode<Elemplent> *right,*down;

	CCrossNode();
	CCrossNode(const CTriple<Elemplent> & tempctriple,
		CCrossNode<Elemplent>* tempright = NULL,CCrossNode<Elemplent>*tempdown = NULL);
};

//构造函数
template <class Elemplent>
CCrossNode<Elemplent>::CCrossNode()
{
	right= NULL;
	down = NULL;
}

//构造函数
template <class Elemplent>
CCrossNode<Elemplent>::CCrossNode(const CTriple<Elemplent> & tempctriple, CCrossNode<Elemplent>* tempright /* = NULL */,CCrossNode<Elemplent>*tempdown /* = NULL */)
{
	nodectriple.cols = tempctriple.cols;
	nodectriple.rows = tempctriple.rows;
	nodectriple.value = tempctriple.value;
	right = tempright;
	down = tempdown;
}

//十字链表类定义
template<class Elemplent>
class CCrossSparseMatrix
{
protected:
	int row,col,num;
	CCrossNode<Elemplent> **rightHead,**downHead;
public:
	CCrossSparseMatrix(int temprow =111,int tempcol =111);
	CCrossSparseMatrix(const CCrossSparseMatrix<Elemplent> & tempcopy);
	CCrossSparseMatrix<Elemplent> &operator = (const CCrossSparseMatrix<Elemplent> & tempcopy);
	~CCrossSparseMatr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值