第七章(1).图的数组(邻接矩阵)存储表示

//图形结构的特点是:节点之间的关系可以是任意的//

#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < limits.h >

#define TRUE 1  
#define FALSE 0    
#define OK 1
#define ERROR 0
#define OVERFLOW -2     

typedef int Status ;

//---------------图的数组(邻接矩阵)存储表示------------------//

#define INFINITY INT_MAX    //最大值(无穷大)INT_MAX = 2147483647
#define MAX_VERTEX_NUM 20    //最大顶点个数(vertex顶点)
#define MAX_INFO 20      //关于边的信息的字符串长度
#define MAX_NAME 5      //关于顶点信息的字符串长度

typedef int VRType ;     //此处考虑无权图
typedef char InfoType ;
//typedef char* VertexType ;
typedef char VertexType[MAX_NAME];  //VertexType可以根据实际情况灵活设定类型!int,float,char…

typedef enum{ DG , DN , UDG , UDN }GrapKind ; //{有向图(Digraph),有向网(Digraph Network), 无向图(Undigraph),无向网(Undigraph Network)}

typedef struct ArcCell
{
 VRType adj ;  //VRType是顶点关系类型。对于无权图,用1或0表示相邻否。对于带权图,则为权值类型。
 InfoType *info ; //该弧(Arc)相关的信息的指针
} ArcCell , AdjMatrix[ MAX_VERTEX_NUM ][ MAX_VERTEX_NUM ] ;

typedef struct
{
 VertexType vexs[ MAX_VERTEX_NUM ] ; //顶点向量
 AdjMatrix arcs ;     //邻接矩阵
 int vexnum , arcnum ;    //图的当前顶点数和弧树
 GrapKind kind ;
} MGraph ;

//-------------------------Link Queue---------------------------//

typedef VRType QElemType ;

typedef struct QNode               //链结点
{
 QElemType data ;
 struct QNode *next ;
} QNode , *QueuePtr ;

typedef struct                   
{
 QueuePtr front ;            //队头指针   队头出元素
 QueuePtr rear ;             //队尾指针   队尾进元素
} LinkQueue ;

Status InitQueue( LinkQueue *Q ) ;
Status EnQueue( LinkQueue *Q , QElemType e ) ;
Status DeQueue( LinkQueue *Q , QElemType *e ) ;
Status QueueEmpty( LinkQueue Q ) ;

//-------------------------------------------------------------//

//--------------------Basic Fuction---------------------//

Status InitGraph( MGraph *G )
{
 int i , j ;
 char gn[ 5 ] ;
 
 printf( "It's a graph or a net (Input :graph or net):" ) ;
 gets( gn ) ;
 
 printf( "Input the number of Vertex and Arc : " ) ;
 scanf( "%d %d" , &( *G ).vexnum , &( *G ).arcnum ) ;  
 
 printf("Input the vector of %d vex( %d char ): \n" , ( *G ).vexnum , MAX_NAME ) ;
 for( i = 0 ; i < ( *G ).vexnum ; ++ i )  //构造顶点向量  Input:a b c d …(输入字符处理)
 {
  scanf( "%s" , ( *G ).vexs[ i ] ) ;  //访存错误,因为无内存。刚开始定义typedef char* VertexType;  所以( *G ).vexs[ i ]只是一个没有空间的指针
 }       
 
 if( strcmp( gn , "net" ) == 0 )
 {
  for( i = 0 ; i < ( *G ).vexnum ; ++ i )  //初始化邻接矩阵
  {
   for( j = 0 ; j < ( *G ).vexnum ; ++ j )
   {   
    ( *G ).arcs[ i ][ j ].adj =  INFINITY ; //{adj , info}网  INFINITY等效于无穷大  
    ( *G ).arcs[ i ][ j ].info = NULL ;
   }
  }
 }
 else
 {
  if( strcmp( gn , "graph" ) == 0 )
  {
   for( i = 0 ; i < ( *G ).vexnum ; ++ i )  
   {
    for( j = 0 ; j < ( *G ).vexnum ; ++ j )
    {   
     ( *G ).arcs[ i ][ j ].adj = 0 ;  //图
     ( *G ).arcs[ i ][ j ].info = NULL ;
    }
   }
  }
  else
   return ERROR ;
 }
 return OK ;
}

Status InputInformation( MGraph *G , int i , int j )
{
 char s[ MAX_INFO ] , *info ;
 int w ;
 
 printf("请输入该边的相关信息(%d个字符): ", MAX_INFO ) ;
 gets( s );
 w = strlen( s ) ;
 if( w )
 {
  info = ( char * )malloc( ( w + 1 ) * sizeof( char ) ) ;
  strcpy( info , s ) ;
  ( *G ).arcs[ i ][ j ].info = info ;
 }
 return OK ;
}

Status LocateVex( MGraph G , VertexType u ) //若G中存在顶点u,则返回该顶点在图中位置;否则返回-1 
{
 int i ;
 
 for( i = 0 ; i < G.vexnum ; ++ i )
 {
  if( strcmp( u , G.vexs[ i ] ) == 0 )
   return i ;
 }
 return EOF ;   //EOF means -1 .
}

//----------------------Create Fuction-----------------------------//

Status CreateUDN( MGraph *G )  //构造无向网*G
{
 int i , j , k , w ;
 int IncInfo ;
 VertexType va , vb ; 
 // va = vb = NULL ;  //typedef char VertexType[MAX_NAME];后,va,vb实际上是一个数组名,有空间!而指针则需要初始化
 
 InitGraph( G ) ;
 printf( "It's there any information for arc?( 1 means yes ): " ) ;
 scanf( "%d" , &IncInfo ) ;
 printf( "请输入%d条弧的边尾、边头以及权值: \n" ,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值