# 1.

//稀疏矩阵三元组顺序表示
#include <iostream>
using namespace std;

#define capacity 1024
typedef int Datatype;//矩阵中的元素类型
struct TriType
{
int row,col;
Datatype value;
};

class TriArray
{
public:
TriArray(Datatype *arr,int r,int c);
virtual ~TriArray();
bool GetValue(Datatype &element,int r,int c)const;
void Assign(Datatype element,int r,int c);
private:
TriType *m_pTriArray;
int rows;
int cols;
int m_isize;
};

TriArray::TriArray(Datatype *arr,int r,int c)
{
rows = r;
cols = c;
m_pTriArray = new TriType[capacity];
m_isize = 0;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
if(*(arr+i*c+j) != 0)
{
m_pTriArray->row = i +1;
m_pTriArray->col = j +1;
m_pTriArray->value = *(arr+i*c+j);
m_isize ++;
}
}
}

}

TriArray::~TriArray()
{
delete []m_pTriArray;
m_pTriArray = NULL;
}

bool TriArray::GetValue(Datatype &element,int r,int c)const
{
if (r <= 0 || c <= 0 || r > rows || c > cols)
{
cout << "parament error !" << endl;
return false;
}
int i = 0;
while(i < m_isize)
{
if(m_pTriArray[i].row == r && m_pTriArray[i].col == c)
{
element = m_pTriArray[i].value;
return true;;
i ++;
}
return false;
}

void TriArray::Assign(Datatype element,int r,int c)
{
if (r <= 0 || c <= 0 || r > rows || c > cols) cout << "parament error !" << endl;
while(i < rows && r > m_pTriArray[i].row ) i ++;
while(i < cols && c > m_pTriArray[i].col ) i ++;
if(m_pTriArray[i].row == r && m_pTriArray[i].col == c)
{
m_pTriArray[i].value = element;
}
else
{
for (int j = m_isize - 1; j >= i; ++i)
{
m_pTriArray[j+1].row = m_pTriArray[j].row;
m_pTriArray[j+1].col = m_pTriArray[j].col;
m_pTriArray[j+1].value = m_pTriArray[j].value;
}
m_pTriArray[i].row = r;
m_pTriArray[i].col = c;
m_pTriArray[i].value = element;
m_isize ++;
}
}

int FastTransMatrix(TriArray TA,TriArray *TB)
{
TB->rows = TA.cols;
TB->cols = TA.rows;
TB->m_isize = TA.m_isize;
int pos[TA.cows] = { 0 },num[TA.cows] = { 0 };
if (TA.m_isize == 0) cout << "Matrix is Empty!" << endl;
for (int i = 0; i < TA.m_isize; ++i) ++ num[TA.m_pTriArray[i].col] ;
pos[1] = 1;
for (int j = 2;j <= TA.cols; ++j) num[j] = pos[j - 1] + num[j - 1];
for(int i = 0;i < TA.m_isize;i ++)
{
j = TA.m_pTriArray[i].col;
k = pos[j];
TB->m_pTriArray[k -1].row = TA.m_pTriArray[i].row;
TB->m_pTriArray[k -1].col = TA.m_pTriArray[i].col;
TB->m_pTriArray[k -1].value = TA.m_pTriArray[i].value;
pos[j] ++;
}


# 2.十字链表

//十字链表
#include <stdio.h>

typedef int DataType;
typedef struct Node
{
int row,col;
DataType value;
struct Node *down,*right;
} CNode,*PNode;

typedef struct
{
PNode *m_prow;
PNode *m_pcol;
int rows,cols,nums;

{
if((*CL) == NULL) {printf("Error!");return -1;}
(*CL)->rows = r;
(*CL)->cols = c;
(*CL)->nums = 0;
(*CL)->m_prow = (PNode *)malloc(r*sizeof(PNode));
if((*CL)->m_prow == NULL) {printf("Error!");return -1;}
for (int i = 0; i < r; ++i) (*CL)->m_prow[i] = NULL;
(*CL)->m_pcol = (PNode *)malloc(c*sizeof(PNode));
if((*CL)->m_pcol == NULL) {printf("Error!");return -1;}
for (int i = 0; i < c; ++i) (*CL)->m_pcol[i] = NULL;
PNode p,q;
for (int j = 0; j < r; ++j)
for (int j = 0; j < c; ++j)
{
if(*(array+i*r+j) != 0)
{
p = (PNode)malloc(sizeof(CNode));
p->row = i + 1;
p->col = j + 1;
p->value = *(array+i*r+j);
p->down = NULL;
P->right = NULL;
(*Cl)->nums ++;
if((*CL)->m_prow[i]!=NULL)
{
q = (*CL)->m_prow[i];
while(q->right != NULL && q->col < j + 1)
q = q->right;
p -> right = q ->right;
q -> right = p;
}
else (*CL)->m_prow[i] = p;
if((*CL)->m_pcol[j]!= NULL)
{
q = (*CL)->m_pcol[j];
while(q->down != NULL && q->row < i + 1)
q = q->down;
p->down = q->down;
q->down = p;
}
else (*CL)->m_pcol[j]=p;
}
}
return 1;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120