稀疏矩阵的三元组表示算法
# include <stdio.h>
# define M 6
# define N 7
# define MaxSize 100
typedef int ElemType;
typedef struct
{
int r;
int c;
ElemType d;
} TupNode;
typedef struct
{
int rows;
int cols;
int nums;
TupNode data[ MaxSize] ;
} TSMatrix;
void CreatMat ( TSMatrix & t, ElemType A[ M] [ N] )
{
int i, j;
t. rows= M; t. cols= N; t. nums= 0 ;
for ( i= 0 ; i< M; i++ )
{
for ( j= 0 ; j< N; j++ )
if ( A[ i] [ j] != 0 )
{
t. data[ t. nums] . r= i; t. data[ t. nums] . c= j;
t. data[ t. nums] . d= A[ i] [ j] ; t. nums++ ;
}
}
}
bool Value ( TSMatrix & t, ElemType x, int i, int j)
{
int k= 0 , k1;
if ( i>= t. rows || j>= t. cols)
return false;
while ( k< t. nums && i> t. data[ k] . r) k++ ;
while ( k< t. nums && i== t. data[ k] . r && j> t. data[ k] . c) k++ ;
if ( t. data[ k] . r== i && t. data[ k] . c== j)
t. data[ k] . d= x;
else
{
for ( k1= t. nums- 1 ; k1>= k; k1-- )
{
t. data[ k1+ 1 ] . r= t. data[ k1] . r;
t. data[ k1+ 1 ] . c= t. data[ k1] . c;
t. data[ k1+ 1 ] . d= t. data[ k1] . d;
}
t. data[ k] . r= i; t. data[ k] . c= j; t. data[ k] . d= x;
t. nums++ ;
}
return true;
}
bool Assign ( TSMatrix t, ElemType & x, int i, int j)
{
int k= 0 ;
if ( i>= t. rows || j>= t. cols)
return false;
while ( k< t. nums && i> t. data[ k] . r) k++ ;
while ( k< t. nums && i== t. data[ k] . r && j> t. data[ k] . c) k++ ;
if ( t. data[ k] . r== i && t. data[ k] . c== j)
x= t. data[ k] . d;
else
x= 0 ;
return true;
}
void DispMat ( TSMatrix t)
{
int i;
if ( t. nums<= 0 )
return ;
printf ( "\t%d\t%d\t%d\n" , t. rows, t. cols, t. nums) ;
printf ( "\t------------------\n" ) ;
for ( i= 0 ; i< t. nums; i++ )
printf ( "\t%d\t%d\t%d\n" , t. data[ i] . r, t. data[ i] . c, t. data[ i] . d) ;
}
void TranTat ( TSMatrix t, TSMatrix & tb)
{
int p, q= 0 , v;
tb. rows= t. cols; tb. cols= t. rows; tb. nums= t. nums;
if ( t. nums!= 0 )
{
for ( v= 0 ; v< t. cols; v++ )
for ( p= 0 ; p< t. nums; p++ )
if ( t. data[ p] . c== v)
{
tb. data[ q] . r= t. data[ p] . c;
tb. data[ q] . c= t. data[ p] . r;
tb. data[ q] . d= t. data[ p] . d;
q++ ;
}
}
}
int main ( )
{
TSMatrix t, tb;
int x, y= 10 ;
int A[ 6 ] [ 7 ] = {
{ 0 , 0 , 1 , 0 , 0 , 0 , 0 } ,
{ 0 , 2 , 0 , 0 , 0 , 0 , 0 } ,
{ 3 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 5 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 6 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 7 , 4 } } ;
CreatMat ( t, A) ;
printf ( "b:\n" ) ; DispMat ( t) ;
if ( Assign ( t, x, 2 , 5 ) == true)
printf ( "Assign(t,x,2,5)=>x=%d\n" , x) ;
else
printf ( "Assign(t,x,2,5)=>参数错误\n" ) ;
Value ( t, y, 2 , 5 ) ;
printf ( "执行Value(t,10,2,5)\n" ) ;
if ( Assign ( t, x, 2 , 5 ) == true)
printf ( "Assign(t,x,2,5)=>x=%d\n" , x) ;
else
printf ( "Assign(t,x,2,5)=>参数错误\n" ) ;
printf ( "b:\n" ) ; DispMat ( t) ;
TranTat ( t, tb) ;
printf ( "tb:\n" ) ; DispMat ( tb) ;
return 1 ;
}
稀疏矩阵的十字链表表示
# include <stdio.h>
# include <malloc.h>
# define M 3
# define N 4
# define Max ( ( M) > ( N) ? ( M) : ( N) )
typedef int ElemType;
typedef struct mtxn
{
int row;
int col;
struct mtxn * right, * down;
union
{
ElemType value;
struct mtxn * link;
} tag;
} MatNode;
void CreatMat ( MatNode * & mh, ElemType a[ ] [ N] )
{
int i, j;
MatNode * h[ Max] , * p, * q, * r;
mh= ( MatNode * ) malloc ( sizeof ( MatNode) ) ;
mh-> row= M; mh-> col= N;
r= mh;
for ( i= 0 ; i< Max; i++ )
{
h[ i] = ( MatNode * ) malloc ( sizeof ( MatNode) ) ;
h[ i] -> down= h[ i] -> right= h[ i] ;
r-> tag. link= h[ i] ;
r= h[ i] ;
}
r-> tag. link= mh;
for ( i= 0 ; i< M; i++ )
{
for ( j= 0 ; j< N; j++ )
{
if ( a[ i] [ j] != 0 )
{
p= ( MatNode * ) malloc ( sizeof ( MatNode) ) ;
p-> row= i; p-> col= j; p-> tag. value= a[ i] [ j] ;
q= h[ i] ;
while ( q-> right!= h[ i] && q-> right-> col< j)
q= q-> right;
p-> right= q-> right; q-> right= p;
q= h[ j] ;
while ( q-> down!= h[ j] && q-> down-> row< i)
q= q-> down;
p-> down= q-> down; q-> down= p;
}
}
}
}
void DestroyMat ( MatNode * & mh)
{
MatNode * pre, * p, * mp;
mp= mh-> tag. link;
while ( mp!= mh)
{
pre= mp-> right;
if ( pre!= mp)
{
p= pre-> right;
while ( p!= mp)
{
free ( pre) ;
pre= p; p= p-> right;
}
}
mp= mp-> tag. link;
}
pre= mh-> tag. link;
p= pre-> tag. link;
while ( p!= mh)
{
free ( pre) ;
pre= p; p= p-> tag. link;
}
free ( mh) ;
}
void DispMat ( MatNode * mh)
{
MatNode * p, * q;
printf ( "行=%d 列=%d\n" , mh-> row, mh-> col) ;
p= mh-> tag. link;
while ( p!= mh)
{
q= p-> right;
while ( p!= q)
{
printf ( "%d\t%d\t%d\n" , q-> row, q-> col, q-> tag. value) ;
q= q-> right;
}
p= p-> tag. link;
}
}
int main ( )
{
ElemType a[ M] [ N] = { { 1 , 0 , 0 , 2 } , { 0 , 0 , 3 , 0 } , { 0 , 0 , 0 , 4 } } ;
MatNode * mx;
CreatMat ( mx, a) ;
printf ( "a的十字链表:\n" ) ; DispMat ( mx) ;
DestroyMat ( mx) ;
return 1 ;
}
广义表基本运算算法
# include <stdio.h>
# include <malloc.h>
typedef char ElemType;
typedef struct lnode
{
int tag;
union
{
ElemType data;
struct lnode * sublist;
} val;
struct lnode * link;
} GLNode;
int GLLength ( GLNode * g)
{
int n= 0 ;
GLNode * g1;
g1= g-> val. sublist;
while ( g1!= NULL )
{
n++ ;
g1= g1-> link;
}
return n;
}
int GLDepth ( GLNode * g)
{
GLNode * g1;
int maxd= 0 , dep;
if ( g-> tag== 0 )
return 0 ;
g1= g-> val. sublist;
if ( g1== NULL )
return 1 ;
while ( g1!= NULL )
{
if ( g1-> tag== 1 )
{
dep= GLDepth ( g1) ;
if ( dep> maxd)
maxd= dep;
}
g1= g1-> link;
}
return ( maxd+ 1 ) ;
}
GLNode * CreateGL ( char * & s)
{
GLNode * g;
char ch= * s++ ;
if ( ch!= '\0' )
{
g= ( GLNode * ) malloc ( sizeof ( GLNode) ) ;
if ( ch== '(' )
{
g-> tag= 1 ;
g-> val. sublist= CreateGL ( s) ;
}
else if ( ch== ')' )
g= NULL ;
else if ( ch== '#' )
g= NULL ;
else
{
g-> tag= 0 ;
g-> val. data= ch;
}
}
else
g= NULL ;
ch= * s++ ;
if ( g!= NULL )
if ( ch== ',' )
g-> link= CreateGL ( s) ;
else
g-> link= NULL ;
return g;
}
void DestroyGL ( GLNode * & g)
{
GLNode * g1, * g2;
g1= g-> val. sublist;
while ( g1!= NULL )
{
if ( g1-> tag== 0 )
{ g2= g1-> link;
free ( g1) ;
g1= g2;
}
else
{ g2= g1-> link;
DestroyGL ( g1) ;
g1= g2;
}
}
free ( g) ;
}
void DispGL ( GLNode * g)
{
if ( g!= NULL )
{
if ( g-> tag== 0 )
printf ( "%c" , g-> val. data) ;
else
{
printf ( "(" ) ;
if ( g-> val. sublist== NULL )
printf ( "#" ) ;
else
DispGL ( g-> val. sublist) ;
printf ( ")" ) ;
}
if ( g-> link!= NULL )
{
printf ( "," ) ;
DispGL ( g-> link) ;
}
}
}