数据结构经典算法汇总___图的邻接矩阵实现

图的邻接矩阵实现:

Graph.h 文件:

#include<iostream.h>

#include"seqlist.h"

template<typename VertexType>class SeqList;

const int MaxVertexes= 20 ;  //max vertexes

template<typename VertexType,typename ArcType>class Graph{

       friend class SeqList<VertexType>;

       private:

              ArcType arcs[MaxVertexes][MaxVertexes];

              SeqList<VertexType> vertexes;    // vertexes sequence

              int currentarcsize;

 

       public:

              Graph(){

                     for(int i=0;i<MaxVertexes;i++)

                            for(int j=0;j<MaxVertexes;j++)

                                   arcs[i][j]=0;

              }

              int InsertVertexes(VertexType &v);

              int InsertArc(int v1,int v2); //insert one border in num of vertexes v1 to v2

              int Delete(int v1,int v2);    //delete the border

              int Delete(int v);            //delete the vertex and the border

              int GetVertexNum(){

                     return vertexes.GetLen();

              }

              void PrintGraphVertex(){

                     cout<<"the graph's vertexes is:"<<endl;

                     vertexes.Print();

              }

              void PrintGraphArc(){

                     int j,max=vertexes.GetLen();

                     cout<<"the grpah's arc is:"<<endl;

                     for(int i=0;i<max;i++){

                            for(j=0;j<max;j++)

                                   cout<<arcs[i][j]<<" ";

                            cout<<endl;

                     }

              }

};

 

template<typename VertexType,typename ArcType>int Graph<VertexType, ArcType>::InsertVertexes(VertexType &v){

       return vertexes.Insert(v);

}

template<typename VertexType,typename ArcType>int Graph<VertexType, ArcType>::InsertArc(int v1,int v2){

       if(v1<0 ||v1>vertexes.GetLen()-1||v2<0 ||v2>vertexes.GetLen()-1){

              cout<<"the vertex is not exits:"<<endl;return 0;

       }

       arcs[v1][v2]=1;

       currentarcsize++;

       return 1;

}

 

主测试函数:

Main.cpp

Int main()

{

Graph<char,int> graph;

   char a[3],&b=a[0];

   int v1[4]={0,0,1,2},v2[4]={1,2,2,1};

   for(int i=0;i<3;i++){

          cin>>a[i];

          b=a[i];

       graph.InsertVertexes(b);

   }

   for(i=0;i<4;i++){

       graph.InsertArc(v1[i],v2[i]);

   }

      

   //cout<<graph.InsertVertexes(b)<<endl;

   graph.PrintGraphVertex(); cout<<endl;

   //graph.PrintGraphArc();

   //graph.InsertArc(0,0);

   graph.PrintGraphArc();

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值