1. /* 
  2.     名称:图的数组存储实现 
  3. */ 
  4. #include<iostream> 
  5. #include<assert.h> 
  6. #include<malloc.h> 
  7. #define  N      5       //图的节点个数  
  8. #define  E      4       //图的边的条数 
  9. using namespace std; 
  10. typedef   int Type; 
  11. typedef  struct  Craphnode 
  12.       Type  node_container; 
  13.       Type  node_index; 
  14. }GraphNode; 
  15. class Graph 
  16.       public
  17.              Graph(); 
  18.              ~Graph(); 
  19.              void CreateGraph(); 
  20.              void DFS(int i); 
  21.              void Display(); 
  22.              void setarc(); 
  23.       private
  24.               Type  arc[N][N];  //图的弧信息 
  25.               GraphNode Vnode;  //图的节点信息  
  26.               int snode_index,enode_index; //图的弧的开始节点索引和结束节点索引          
  27. };    
  28. Graph::Graph()    //图的弧初始化 
  29.       for(int i=0;i<N;i++)          
  30.       { 
  31.               for(int j=0;j<N;j++) 
  32.               {  
  33.                  arc[i][j]=0;       
  34.               } 
  35.       } 
  36. void Graph::CreateGraph() 
  37.       std::cout<<"Input:"<<N<<"顶点信息"<<endl; 
  38.       for(int i=0;i<N;i++) 
  39.       {  
  40.                std::cout<<"输入Vnode.index:"<<endl; 
  41.                std::cin>>Vnode.node_index; 
  42.                std::cout<<"输入Vnode.container:"<<endl; 
  43.                std::cin>>Vnode.node_container; 
  44.      } 
  45.       std::cout<<"图的顶点信息录入完毕!"<<endl; 
  46.       for(int i=0;i<E;i++) 
  47.       { 
  48.               setarc(); 
  49.               while(snode_index>E,enode_index>E) 
  50.               { 
  51.                 std::cout<<"error!"
  52.                 setarc(); 
  53.               } 
  54.               arc[snode_index][enode_index]=1;                                     
  55.       } 
  56.       
  57. }//end of CreateGraph function definition 
  58. Graph::~Graph() 
  59. void Graph::setarc() 
  60.               std::cout<<"起点:"
  61.               std::cin>>snode_index; 
  62.               std::cout<<endl<<"终点:"
  63.               std::cin>>enode_index; 
  64. void Graph::Display() 
  65. {    
  66.       std::cout<<endl; 
  67.       for(int i=0;i<N;i++)          
  68.       { 
  69.               for(int j=0;j<N;j++) 
  70.               {  
  71.                  std::cout<<arc[i][j];       
  72.               } 
  73.               std::cout<<endl; 
  74.       } 
  75.       getchar(); 
  76.  } 
  77. Graph   g; 
  78. int main() 
  79.     g.CreateGraph(); 
  80.     g.Display(); 
  81.     getchar(); 
  82.     return 0; 
  83. }