首页 > 代码库 > 堆栈队列和数组-行逻辑链接稀疏矩阵

堆栈队列和数组-行逻辑链接稀疏矩阵

#include<iostream>#include <iomanip>#include"windows.h"using namespace std;struct Tripple{    int x,y,value;};struct RLSMatrix{    int r,c,cnt;    Tripple* tripples;    int* rpos;};RLSMatrix* createRLSMatrix(int r,int c,int maxCnt){    RLSMatrix* p = (RLSMatrix*)malloc(sizeof(RLSMatrix));    p->r=r;    p->c=c;    p->cnt=0;    p->tripples=new Tripple[maxCnt];    p->rpos=new int[p->r+1];    p->rpos[0]=0;    return p;}RLSMatrix* transpose(RLSMatrix*  src){    RLSMatrix* ans =createRLSMatrix(src->c,src->r,src->cnt);    int* ccnts = new int[src->c+1];    int* cpos = new int[src->c+1];    cpos[0]=0;    for(int i=0;i<=src->c;i++)        ccnts[i]=0;    for(int i=0;i<src->cnt;i++)        ccnts[src->tripples[i].y]++;    for(int i=1;i<=src->c;i++)    {        ans->rpos[i]=ans->rpos[i-1]+ccnts[i-1];        cpos[i] = ans->rpos[i];    }    delete[] ccnts;    Tripple newTripple;    for(int i=0;i<src->cnt;i++)    {        newTripple.x=src->tripples[i].y;        newTripple.y=src->tripples[i].x;        newTripple.value=src->tripples[i].value;        ans->tripples[cpos[newTripple.x]++]=newTripple;        ans->cnt++;    }    return ans;}void output(RLSMatrix* src){    int cur=0;    for(int i=1;i<=src->r;i++)    {        for(int j=1;j<=src->c;j++)        {            if(src->tripples[cur].x==i && src->tripples[cur].y==j)            {                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//彰显非0元                cout<<src->tripples[cur].value<<"   ";                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//设置三色相加                cur++;            }            else cout<<"0   ";        }        cout<<endl;    }}void main(){     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//设置三色相加     int r=11,c=18,maxCnt=45;     RLSMatrix* m= createRLSMatrix(r,c,maxCnt);     int lstR=0;     for(int i=1;i<=r;i++)     {         for(int j=1;j<=c;j++)         {             if(rand()%10==1 && m->cnt<maxCnt)             {                 m->tripples[m->cnt].x=i;                 m->tripples[m->cnt].y=j;                 m->tripples[m->cnt].value=http://www.mamicode.com/rand()%10;                 if(i!=lstR)                      m->rpos[i]=m->cnt;                 m->cnt++;             }         }         lstR=i;     }     output(m);     cout<<endl;     output(transpose(m));     cin>>r;}

 

堆栈队列和数组-行逻辑链接稀疏矩阵