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

来源:互联网 发布:清风知我意匪我思存 编辑:程序博客网 时间:2024/05/16 19:31
#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);~CCrossSparseMatrix();void DestroyAll();public:void DistroyMatrix();bool InsertNode(const CTriple<Elemplent> &temptriple);int Getrow()const;int Getcol()const;int Getnum()const;bool SetElemplent(int r,int c,const Elemplent & tempelemplent);bool GetElemplent(int r,int c,Elemplent &tempeleplent);};//构造函数template<class Elemplent>CCrossSparseMatrix<Elemplent>::CCrossSparseMatrix(){row = 0;col = 0;num = 0;rightHead = NULL;downHead = NULL;}//构造函数template<class Elemplent>CCrossSparseMatrix<Elemplent>::CCrossSparseMatrix(int temprow /* =111 */,int tempcol /* =111 */){row = temprow;col = tempcol;num = 0;//行列的两个指针数组,用到二级指针rightHead =new CCrossNode<Elemplent> *[row+1];downHead = new CCrossNode<Elemplent> *[col+1];//初始化for (int i = 1;i<=row;i++){rightHead[i] = NULL;}for (int j = 1;j<=col ;j++){downHead[i]=NULL;}}//销毁所有节点,析构函数用到template <class Elemplent>void CCrossSparseMatrix<Elemplent>::DestroyAll(){int i;for (i = 1;i<row;i++){if (rightHead[i] !=NULL){CCrossNode<Elemplent> *ptr = rightHead[i];rightHead[i] = ptr->right;delete ptr;}}delete []rightHead;delete []downHead;}//析构函数template <class Elemplent>CCrossSparseMatrix::~CCrossSparseMatrix(){DestroyAll();}//得到列数template <class Elemplent>int CCrossSparseMatrix<Elemplent>::Getcol()const{return col;}//得到行数template <class Elemplent>int CCrossSparseMatrix<Elemplent>::Getrow()const{return row;}//得到非零元总数template <class Elemplent>int CCrossSparseMatrix<Elemplent>::Getnum()const{return num;}//设置元素值template <class Elemplent>bool CCrossSparseMatrix<Elemplent>::SetElemplent(int r,int c,const Elemplent &tempeleplent){if (r>row || c>col || c<1 || r <1){return false;}CTriple<Elemplent> e(r,c,tempeleplent);CCrossNode<Elemplent> *preptr,*ptr;CCrossNode<Elemplent> *newptr = new CCrossNode<Elemplent>(e);//如果在列数的开始处if (rightHead[r] == NULL || c<rightHead[r]->nodectriple.cols ){newptr->right = rightHead[r];rightHead[r] = newptr;}else{preptr = NULL; ptr = rightHead[r];//往下遍历while(ptr!=NULL && ptr->nodectriple.cols < c){preptr = ptr; ptr=ptr->right ;}if (ptr !=NULL && ptr->nodectriple.cols == c&& ptr->nodectriple.rows = r){//如果设置元素不为空,赋值if (tempeleplent  != 0){ptr->nodectriple.value = tempeleplent;//释放空间,前面定义的,用不到delete newptr;}//如果为空,删除节点else{preptr->right = ptr->right;//释放空间,前面定义的,用不到delete newptr;}}//查找不到节点,建立一个新节点插进去else{preptr->right = newptr; newptr->right = ptr;}}//是不是在行数的第一个if (downHead[c]==NULL || downHead[c]->nodectriple.rows >=r){newptr->down = downHead[c];downHead[c] = newptr;num++;return true;}else{preptr =NULL; ptr = downHead[c];//往下遍历while(ptr!=NULL && ptr->nodectriple.cols < c){preptr = ptr; ptr = ptr->down;}if (ptr !=NULL && ptr->nodectriple.cols = c && ptr->nodectriple.rows = r){//如果元素之为空,删除节点,非零元--if (tempeleplent == 0){preptr->down = ptr->down;delete newptr;num--;return true;}//元素不为空,赋值else{ptr->nodectriple.value = tempeleplent;delete newptr;return true;}}//查找不到位置,说明不存在,添加一个节点,非零元++else{preptr->down = newptr;newptr->down = ptr;num++;return true;}}return false;}//得到元素值template<class Elemplent>bool CCrossSparseMatrix<Elemplent>::GetElemplent(int r,int c, Elemplent & tempelemplent){if (r <1 || c<1 ||r>row || c> col ){return false;}CCrossNode<Elemplent> * ptr = rightHead[r];//遍历while(ptr !=NULL && ptr->nodectriple.cols <c){ptr= ptr->right;}//找到if (ptr!=NULL && ptr->nodectriple.cols == c && ptr->nodectriple.rows == r){tempelemplent = ptr->nodectriple.value;return true;}else {tempelemplent = 0;return false;}}//通过三元组,插入,构造函数用到,和设置一个元素差不多,不在注释template <class Elemplent>bool CCrossSparseMatrix<Elemplent>::InsertNode(const CTriple<Elemplent> &temptriple){if (temptriple.cols>col || temptriple >row || temptriple<1 ||temptriple <1){return false;}int temprow = temptriple.rows;int tempcol = temptriple.cols;CCrossNode<Elemplent> *preptr,*ptr;CCrossNode<Elemplent> *newptr = new CCrossNode<Elemplent>(temptriple);if (rightHead[temprow] == NULL && rightHead[temprow]->nodectriple.cols >= tempcol){newptr->right = rightHead[temprow];rightHead[temprow] = newptr;}else{preptr = NULL;ptr = rightHead[temprow];while(ptr!=NULL && ptr->nodectriple.cols < tempcol){preptr = ptr;ptr=ptr->right;}if (ptr!=NULL  && ptr->nodectriple.rows == temprow && ptr->nodectriple.cols == tempcol){return false;}else{preptr->right = newptr;newptr->right = ptr;}}if (downHead[tempcol] == NULL && downHead[tempcol]->nodectriple.rows > temprow){newptr->down = downHead[tempcol];downHead[tempcol]->down = newptr;num++;return true;}else{preptr =NULL ;ptr = downHead[tempcol];while(ptr!=NULL && ptr->nodectriple.rows <temprow){preptr = ptr;ptr = preptr->down;}if (ptr !=NULL && ptr->nodectriple.cols ==tempcol && ptr->nodectriple.rows == temprow){return false;}else{preptr->down = newptr ; newptr->down = ptr;num++;}}}//赋值构造函数template<class Elemplent>CCrossSparseMatrix<Elemplent>::CCrossSparseMatrix(const CCrossSparseMatrix<Elemplent> & tempcopy){row = tempcopy.row;col = tempcopy.col;num = 0;rightHead = new CCrossNode<Elemplent> *[row+1];downHead = new CCrossNode<Elemplent> * [col+1];int i;for (i =1;i<=row;i++){rightHead[i] = NULL;}for (i = 1;i<col;i++){downHead[i] = NULL;}for (i =1;i<row;i++){for(CCrossNode<Elemplent> * node= tempcopy.rightHead[i];node !=NULL;node =node->right){//调用InsertNode(node->nodectriple);}}}//重载=template <class Elemplent>CCrossSparseMatrix<Elemplent> & CCrossSparseMatrix<Elemplent>::operator= (const CCrossSparseMatrix<Elemplent> & tempcopy){if (this == tempcopy){return *this;}row = tempcopy.row;col = tempcopy.col;num = 0;downHead = new CCrossNode<Elemplent> *[col+1];rightHead = new CCrossNode<Elemplent> *[row+1];int i;for (i = 1; i<row ;i++){rightHead[i] = NULL;}for (i = 1;i<col;i++){downHead[i] =NULL;}for (i =1;i<row;i++){for (CCrossNode<Elemplent> * node = tempcopy.rightHead[i];node = node->right;){InsertNode(node->nodectriple);}}return *this;}#endif

原创粉丝点击