码迷,mamicode.com
首页 > 其他好文 > 详细

队列堆栈和数组-十字链表矩阵

时间:2014-09-28 20:39:55      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   for   sp   div   on   

#include<iostream>
#include <iomanip>
#include"windows.h"
using namespace std;
struct OLinMatrixkNode
{
    int data;
    int x,y;
    OLinMatrixkNode* right;
    OLinMatrixkNode* down;
};
struct OLinMatrix
{
    OLinMatrixkNode** RowHead;
    OLinMatrixkNode** ColHead;
    int r,c,cnt;
};
OLinMatrix* createOLinMatrix(int r,int c)
{
    OLinMatrix* ans=(OLinMatrix*)malloc(sizeof(OLinMatrix));
    ans->RowHead=new OLinMatrixkNode*[r+1];
    ans->ColHead=new OLinMatrixkNode*[c+1];
    for(int i=1;i<=r;i++)
        ans->RowHead[i]=NULL;
    for(int i=1;i<=c;i++)
        ans->ColHead[i]=NULL;
    ans->cnt=0;
    ans->r=r;
    ans->c=c;
    return ans;
}

OLinMatrixkNode* createOLinMatrixNonde(int r,int c,int v)
{
    OLinMatrixkNode* ans=(OLinMatrixkNode*)malloc(sizeof(OLinMatrixkNode));
    ans->x=r;
    ans->y=c;
    ans->data=v;
    return ans;
}
void addElement(OLinMatrix* p,int r,int c,int v)
{
    if(v==0)
        return;

    OLinMatrixkNode* newNode = createOLinMatrixNonde(r,c,v);
    if(p->RowHead[r]==NULL || p->RowHead[r]->y > c)
    {
        newNode->right=p->RowHead[r];
        p->RowHead[r]=newNode;
    }
    else 
    {
        OLinMatrixkNode* temp = p->RowHead[r];
        while( temp->right!=NULL && temp->right->y <= c)
            temp=temp->right;
        newNode->right=temp->right;
        temp->right=newNode;
        if(p->ColHead[c]==NULL || p->ColHead[c]->x > r)
        {
            newNode->down=p->ColHead[c];
            p->ColHead[c]=newNode;
        }
        else 
        {
            OLinMatrixkNode* temp = p->ColHead[c];
            while( temp->down!=NULL && temp->down->x <= c)
                temp=temp->down;
            newNode->down=temp->down;
            temp->down=newNode;
        }
    }
    p->cnt++;
}

void output(OLinMatrix* p)
{
    for(int i=1;i<=p->r;i++)
    {
        OLinMatrixkNode* temp = p->RowHead[i];
        for(int j=1;j<=p->c;j++)
        {
            if(temp!=NULL && temp->y==j)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);//彰显非0元
                cout<<std::left<<setw(3)<<temp->data;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);//设置三色相加
                temp=temp->right;
            }
            else
            {
                cout<<std::left<<setw(3)<<0;
            }
        }
        cout<<endl;
    }
}

void main()
{
    int r=7,c=12,cnt=15;
    OLinMatrix*  m= createOLinMatrix(r,c);
    bool v[100][100];
    memset(v,false,sizeof(100));
    for(int i=1;i<=cnt;i++)
    {
        int x=rand()%r+1,y=rand()%c+1,value=rand()%99+1;
        if(v[x][y])
        {
            addElement(m,x,y,value);
            cout<<x<<" "<<y<<" "<<value<<endl;
            v[x][y]=0;
        }
        else 
        {
            i--;
        }
    }
    output(m);
    cin>>r;
}

 

队列堆栈和数组-十字链表矩阵

标签:style   blog   color   io   os   for   sp   div   on   

原文地址:http://www.cnblogs.com/kbyd/p/3998622.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!