题目:根据稀疏矩阵的三元组形式,建立十字链表存储数据
代码:
struct CNode;
typedef struct CNode *CList;
typedef struct CNode *Position;
struct CNode
{
int i, j;
ElementType Element;
Position Right;
Position Down;
}; // 十字链表节点
typedef struct TNode
{
int i, j;
ElementType X;
}Triple;
// 稀疏矩阵的三元组存储数据结构
typedef struct
{
Triple data[MAX_SIZE];
int m, n, size;
}TripleMatrix;
CList InitCList( int n, int m ); // 建立n行m列的矩阵
Position Find( int i, int j, CList L ); // 查找i列j行元素
int Insert( int i, int j, ElementType X, CList L ); // 在i行j列位置插入元素X
int Delete( int i, int j, CList L ); // 删除i行j列的元素
int PrintCList( CList L ); // 打印整矩阵
CList InitCList( int n, int m )
{
// 建立一个n行m列的十字矩阵
// 创建成功返回十字链表头结点,否则返回NULL
CList Head;
Head = (CList)malloc( sizeof( struct CNode ) );
if ( Head == NULL )
{
printf( "Out of space" );
return NULL;
}
Head->i = n;
Head->j = m;
Head->Down = NULL;
Head->Right = NULL;
for ( int i = n; i >= 1; --i )
{
Position TmpCell;
TmpCell = (Position)malloc( sizeof( struct CNode ) );
if ( TmpCell == NULL )
{
printf( "Out of space" );
return Head;
}
TmpCell->i = i;
TmpCell->j = 0;
TmpCell->Right = TmpCell;
TmpCell->Down = Head->Down;
Head->Down = TmpCell;
}
for ( int j = m; j >= 1; --j )
{
Position TmpCell;
TmpCell = (Position)malloc( sizeof( struct CNode ) );
if ( TmpCell == NULL )
{
printf( "Out of space" );
return Head;
}
TmpCell->i = 0;
TmpCell->j = j;
TmpCell->Down = TmpCell;
TmpCell->Right = Head->Right;
Head->Right = TmpCell;
}
return Head;
}
Position Find( int i, int j, CList L )
{
// 查找在位置i行j列的元素,若不存在返回NULL
Position RowHead, Cur;
if ( i <= 0 || j <= 0 || i > L->i || j > L->j )
return NULL;
RowHead = L->Down;
while ( RowHead->i < i )
RowHead = RowHead->Down;
Cur = RowHead;
while ( Cur->j < j && Cur->Right != RowHead )
Cur = Cur->Right;
if ( Cur->i == i && Cur->j == j )
return Cur;
else
return NULL;
}
int Insert( int i, int j, ElementType X, CList L )
{
// 插入元素在位置i行j列上
// 若i行j列上存在元素,则与元素X覆盖存在的元素
Position TmpCell;
Position Pre, Cur;
Position RowHead, ColHead;
if ( i <= 0 || j <= 0 || i > L->i || j > L->j )
return false;
TmpCell = (Position)malloc( sizeof( struct CNode ) );
if ( TmpCell == NULL )
{
printf( "Out of space" );
return false;
}
TmpCell->i = i;
TmpCell->j = j;
TmpCell->Element = X;
if ( Find( i, j, L ) != NULL )
Delete( i, j, L );
RowHead = L->Down;
ColHead = L->Right;
while ( RowHead->i < i ) // 获得第i行链表头结点
RowHead = RowHead->Down;
while ( ColHead->j < j ) // 获得第j列链表头结点
ColHead = ColHead->Right;
Pre = Cur = RowHead;
while ( Cur->j < j && Cur != RowHead )
{
Pre = Cur;
Cur = Cur->Right;
}
TmpCell->Right = Cur;
Pre->Right = TmpCell;
Pre = Cur = ColHead;
while ( Cur->i < i && Cur != ColHead )
{
Pre = Cur;
Cur = Cur->Down;
}
TmpCell->Down = Cur;
Pre->Down = TmpCell;
return true;
}
int Delete( int i, int j, CList L )
{
// 删除i行j列上的元素,若元素不存在不做任何操作,返回false
// 删除成功返回true
Position Pre, Cur;
Position RowHead, ColHead;
if ( i <= 0 || j <= 0 || i > L->i || j > L->j || Find( i, j, L ) == NULL )
return false;
RowHead = L->Down;
ColHead = L->Right;
while ( RowHead->i < i ) // 获得第i行链表头结点
RowHead = RowHead->Down;
while ( ColHead->j < j ) // 获得第j列链表头结点
ColHead = ColHead->Right;
Pre = Cur = RowHead;
while ( Cur->j < j && Cur->Right != RowHead )
{
Pre = Cur;
Cur = Cur->Right;
}
if ( Cur->j != j )
return false;
else
Pre->Right = Cur->Right;
Pre = Cur = ColHead;
while ( Cur->i < i && Cur->Down )
{
Pre = Cur;
Cur = Cur->Down;
}
if ( Cur->i != i )
return false;
else
Pre->Down = Cur->Down;
free( Cur );
return true;
}
TripleMatrix* CreateTripleMatrix( const char *fileName )
{
// 从文件fileName中读取三元组数据并存储在TMatrix中
// 读取成功返回TMatrix,读取失败返回NULL
int n, m, elemNum = 0;
int i, j, X;
FILE *Fp;
TripleMatrix *TMatrix;
Fp = fopen( fileName, "r" );
if ( Fp == NULL )
{
printf( "Read file fail" );
return NULL;
}
TMatrix = (TripleMatrix *)malloc( sizeof( TripleMatrix ) );
if ( TMatrix == NULL )
{
printf( "Out of space" );
return NULL;
}
fscanf( Fp, "%d %d", &n, &m );
TMatrix->n = n;
TMatrix->m = m;
while ( fscanf( Fp, "%d %d %d", &i, &j, &X ) != EOF )
{
TMatrix->data[elemNum].i = i;
TMatrix->data[elemNum].j = j;
TMatrix->data[elemNum].X = X;
elemNum++;
}
TMatrix->size = elemNum;
return TMatrix;
}
CList CreateCList( TripleMatrix *TMatrix )
{
// 从三元组TMatrix中读取数据并创建十字链表存储矩阵
// 返回指向创建的十字链表的头结点的指针
int n, m, elemNum = 0;
CList L;
if ( TMatrix->size == 0 )
{
printf( "No data" );
return NULL;
}
n = TMatrix->n;
m = TMatrix->m;
elemNum = TMatrix->size;
L = InitCList( n, m );
for ( int i = 0; i < elemNum; ++i )
{
int insertSucess = true;
insertSucess = Insert( TMatrix->data[i].i, TMatrix->data[i].j, TMatrix->data[i].X, L );
if ( !insertSucess )
{
printf( "Insert element fail" );
return NULL;
}
}
return L;
}
int PrintCList( CList L )
{
// 以矩阵形式打印十字链表,若链表为空,则返回false
int m, n;
Position RowHead, Cur;
if ( L == NULL )
{
printf( "Empty cross list" );
return false;
}
else
{
m = L->i;
n = L->j;
RowHead = L;
for ( int i = 0; i <= n; ++i )
printf( "%-5d", i );
printf( "\n" );
for ( int i = 1; i <= m; ++i )
{
printf( "%-5d", i );
RowHead = RowHead->Down;
Cur = RowHead->Right;
for ( int j = 1; j <= n; ++j )
{
if ( Cur->j != j )
printf( "%-5d", 0 );
else
{
printf( "%-5d", Cur->Element );
Cur = Cur->Right;
}
}
printf( "\n" );
}
}
return true;
}
TripleMatrix* TransposeTripleMatrix( TripleMatrix *TMatrix )
{
// 对于已经按行序与列序排列的三元组进行转置
// 返回转置后的三元组
int elemNum, colOfMatrix;
int numOfCol[MAX_SIZE];
int posOfFirstElemOfCol[MAX_SIZE];
TripleMatrix *TpMatrix;
if ( TMatrix == NULL || TMatrix->size == 0 )
{
printf( "Matrix is empty" );
return NULL;
}
TpMatrix = (TripleMatrix *)malloc( sizeof( TripleMatrix ) );
if ( TpMatrix == NULL )
{
printf( "Out of space" );
return NULL;
}
posOfFirstElemOfCol[1] = 1;
elemNum = TMatrix->size;
memset( numOfCol, 0, sizeof(numOfCol) );
TpMatrix->m = TMatrix->n;
TpMatrix->n = TMatrix->m;
TpMatrix->size = TMatrix->size;
for ( int i = 0; i < elemNum; i++ )
{
int col = TMatrix->data[i].j;
numOfCol[col]++;
}
colOfMatrix = TMatrix->n;
for ( int col = 2; col <= colOfMatrix; ++col )
posOfFirstElemOfCol[col] = posOfFirstElemOfCol[col - 1] + numOfCol[col - 1];
for ( int i = 0; i < elemNum; ++i )
{
int col = TMatrix->data[i].j;
int pos = posOfFirstElemOfCol[col] - 1;
TpMatrix->data[pos].i = TMatrix->data[i].j;
TpMatrix->data[pos].j = TMatrix->data[i].i;
TpMatrix->data[pos].X = TMatrix->data[i].X;
++posOfFirstElemOfCol[col];
}
return TpMatrix;
}
int OutputTripleMatrixFile( const char *fileName, TripleMatrix *TpMatrix )
{
FILE *fp;
int n, m, size;
int i, j, value;
fp = fopen( fileName, "w" );
if ( fp == NULL )
{
printf( "Write file fail" );
return false;
}
m = TpMatrix->m;
n = TpMatrix->n;
size = TpMatrix->size;
fprintf( fp, "%d %d\n", m, n );
for ( int k = 0; k < size; ++k )
{
i = TpMatrix->data[k].i;
j = TpMatrix->data[k].j;
value = TpMatrix->data[k].X;
fprintf( fp, "%d %d %d\n", i, j, value );
}
return true;
}