稀疏数组类

C++ code

#include "stdafx.h"
#include "LinkList.h"
#include "procs.h"
#pragma once

template <class T>
class CSparseMatrix{
public:
    CSparseMatrix(int row_,int col_)
    {
        row=row_;
        col=col_;
    }
    CSparseMatrix()
    {
        row=5;
        col=5;
    }
    CSparseMatrix(CSparseMatrix &obj)
    {
        row=obj.row;
        col=obj.col;
        for(int i=0;i<obj.ElemList.GetLen();i++)
            ElemList.Add(obj.ElemList[i]);
    }
    void SetElem(int x,int y,T v)
    {
        if(x<0 || y<0 || x>=row || y>=col) return;
        //若存在则覆盖,不存在则按行、列有序插入
        int len=ElemList.GetLen() , i;
        Elem tmp={x,y,v};
        bool flag=0;
        for(i=0;i<len;i++)
        {
            if(ElemList[i].x==x && ElemList[i].y==y)
            {
                ElemList.SetAt(i,tmp);
                flag=1;
                break;
            }
            else if(  (ElemList[i].x==x && ElemList[i].y>y)  ||  (ElemList[i].x>x)  )//行匹配到了,但是列没有匹配到
            {
                ElemList.Insert(i,tmp);//在这条数据之前插入
                flag=1;
                break;
            }
        }
        if(!flag) ElemList.Add(tmp);
    }
    void diaplayList()
    {
        int len=ElemList.GetLen() , i;
        for(i=0;i<len;i++)
            printf("%d,%d,%d\n",ElemList[i].x,ElemList[i].y,ElemList[i].v);
    }
    void displayMatrix()
    {
        T** show=SpaMat2Mat();
        int i , j;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
                printf("%d ",show[i][j]);
            printf("\n");
        }
    }
    T** SpaMat2Mat()
    {
        T ** re;
        int i ,j ;
        re=new T*[row];
        for(i=0;i<row;i++) re[i]=new T[col];
        for(i=0;i<row;i++)
            for(j=0;j<col;j++)
                re[i][j]=NULL;//初始化声明为零
        for(i=0;i<ElemList.GetLen();i++) re[ElemList[i].x][ElemList[i].y]=ElemList[i].v;
        return re;
    }
    CSparseMatrix transpose()
    {
        CSparseMatrix obj;
        obj.col=col;
        obj.row=row;
        Elem empty={0,0,0};
        int i , j , len=ElemList.GetLen();
        for(i=0;i<len;i++) obj.ElemList.Add(empty);//初始化
        int * num=new int[col];
        int * cPot=new int[col];
        zeros(num,col);                //创建好数组后赋值为零
        for(i=0;i<len;i++) num[ElemList[i].y]++;//列数统计
        for(i=0;i<col;i++) cPot[i]=(i ? num[i-1] : 0) + (i ? cPot[i-1] : 0);//对应列下标 标记
        for(i=0;i<len;i++)
        {
            Elem get=ElemList[i];
            Elem set={get.y,get.x,get.v};
            obj.ElemList.SetAt(cPot[get.y]++,set);
        }
        return obj;
    }
    CSparseMatrix Multi(CSparseMatrix right)
    {
        CSparseMatrix<T> obj;
        obj.row=row;
        obj.col=right.col;
        int i , j , k;
        int x , y , v;
        int lastX=ElemList[0].x;
        int * ctemp=new int[right.col];
        zeros(ctemp,right.col);
        for(i=0;i<ElemList.GetLen();i++)
        {
            x=ElemList[i].x;
            lastX=x;
            y=ElemList[i].y;
            v=ElemList[i].v;
            for(j=0;j<right.ElemList.GetLen();j++)//A.y==B.x
                if(right.ElemList[j].x==y)
                    ctemp[right.ElemList[j].y]+= v * right.ElemList[j].v;
            if( i==ElemList.GetLen()-1  ||  lastX!=ElemList[i+1].x )//检测到循环即将结束 或者 行数据即将更新
            {
                for(k=0;k<right.col;k++) 
                    if(ctemp[k])
                    {
                        Elem tmp={x,k,ctemp[k]};
                        obj.ElemList.Add(tmp);
                    }
                zeros(ctemp,right.col);//追加数据完毕,初始化。
            }
        }
        return obj;
    }
protected:
private:
    int row;
    int col;
    typedef struct Elem{
        int x;
        int y;
        T v;
    }Elem;
    CLinkList<Elem> ElemList;
};

 

转载于:https://www.cnblogs.com/TQCAI/p/7606814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值